gpt4 book ai didi

javascript - 在jsp中使用jquery替换div内容

转载 作者:行者123 更新时间:2023-11-30 23:49:13 25 4
gpt4 key购买 nike

到目前为止,我一直在使用以下代码进行 AJAX 调用,将 div 的内容替换为另一个页面:

<script>
function fetchContainerContent(url, containerid) {
var req = false

if (window.ActiveXObject) {
try {
req = new ActiveXObject("Msxml2.XMLHTTP")
} catch (e) {
try {
req = new ActiveXObject("Microsoft.XMLHTTP")
} catch (e) {}
}
} else if (window.XMLHttpRequest) {
req = new XMLHttpRequest()
} else {
return false
}

req.onreadystatechange = function() {
requestContainerContent(req, containerid)
}
req.open('GET', url, true)
req.send(null)
}

function requestContainerContent(req, containerid) {
if (req.readyState == 4 && (req.status==200 || window.location.href.indexOf("http")==-1))
document.getElementById(containerid).innerHTML = req.responseText
}
</script>

我尝试将上面的代码转换为与 jQuery 一起使用,如下所示,但它不起作用。换句话说,我试图模仿上述行为的最终结果,但效果却相差甚远。事实上,屏幕上什么也没有发生,什么也没有改变。我应该提到的是,我实际上并不需要加载...但是由于我看到的示例使用了它,并且由于我不确定如何正确编写 jQuery 语法,因此我将其保留在其中。

<script>
function fetchContainerContent(url, containerid) {
jQuery.ajaxSetup ({
cache: false
});
var ajax_load = "loading...' />";

jQuery("#load_basic").click(function() {
jQuery("#"+containerid).html(ajax_load).load(url);
});
}
</script>

提前致谢。我对 jQuery 很陌生,所以我可能做了一些非常愚蠢的事情。

收到所有评论后(谢谢大家!)我只留下以下内容:

function fetchContainerContent(url, containerid){                   
var ajax_load = "loading...";
$("#load_basic").click(function(){$("#"+containerid).html(ajax_load).load(url);});

}

但我仍然遇到问题,因为它没有更新页面。没有 js 错误,什么也没有发生。

最佳答案

试试这个:

jQuery("#load_basic").click(function() {
jQuery("#result").html(ajax_load).load(url);
return false;
});

请注意点击处理程序末尾的return false 语句。如果 load_basic 是按钮或 anchor 元素,这将阻止传播点击事件。

关于javascript - 在jsp中使用jquery替换div内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1914544/

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