gpt4 book ai didi

javascript - 从不同范围访问新版本的变量 (JavaScript/jQuery)

转载 作者:行者123 更新时间:2023-12-03 08:11:32 25 4
gpt4 key购买 nike

当单击后退按钮时,我想用已加载的变量 data1 填充结果 div。使用此代码,当单击后退按钮时,结果 div 将设置为“”。如何访问新版本的 data1?

Javascript:

function finddata(def,id){
$("#doctitle").html($("#exp").val() + " - Writer's Dictionary");
$("#results").html('<span class="glyphicon glyphicon-cog glyphicon-spin" title="loading..."></span>').fadeIn();
if (def == 1) {
var word = id.innerHTML;
$.get( "processor.php?query=" + word + "&def=" + def, function(data2) {
$("#results").html(data2);
$("#back").click(function(){
$("#results").html(data1);
});
});
} else {
$.get( "processor.php?query=" + $("#exp").val(), function(data1) {
$("#results").html(data1);
});
}
}

HTML:

<div class="container">
<div class="col-md-6 col-md-offset-3" id="results">
<div id="back">Back</div>
</div>
</div>

最佳答案

尝试

<div id="back" onclick="finddata(2)">Back</div> 

def 参数设置为 1 以外的值,该值应在 else 内调用 $.get() > finddata 语句

<小时/>

编辑、更新

var data;

function finddata(def) {
if (def === 1) {
// do stuff
} else {
// if `data` defined
if (data) {
// do not do stuff here
}
// else call `.get()`
else {
var word = id.innerHTML;
$.get( "processor.php?query=" + word + "&def=" + def, function(data2) {
$("#results").html(data2);
$("#back").click(function(){
$("#results").html(data1);
});
});
}
}
}
$("#back").click(function() {
finddata(2)
})

jsfiddle http://jsfiddle.net/229ad2b2/2/

关于javascript - 从不同范围访问新版本的变量 (JavaScript/jQuery),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34124907/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com