![](/img/trans.png)
[英]JS include tag not being removed from dom, even though it has been removed from the file
[英]JQuery event is still being loaded even if the DOM has been removed
我有這個標記
<html>
<body>
<div id ="wrap">
<div id = "main-content">
<!-- more markup here -->
<script>
$(window).scroll(function(){
if($(window).scrollTop() >= ($(document).height() $(window).height())*0.7) {
alert("SCROLLED")
}});
</script>
</div>
</div>
</body>
</html>
而且我通常會進行ajax調用以刪除“主要內容”的內容,而我使用
$.get(sampleUrl, function(data) {
var newTitle = $(data).filter('title').text();
$('#main-content').children().remove();
$('#main-content').html($(data).find('#main-content').html());
},"html");
但是,上面的代碼僅刪除DOM而不刪除諸如“ scroll”事件之類的javascript事件,無論如何,我可以刪除/取消綁定它嗎?
將script
標記放在頁面上時,它將執行。 它將使一個eventListener
在window
上滾動。 刪除main-content
content內部的任何內容都不會撤消偵聽器,並且您的window
顯然仍將存在。
當您想要停止該行為時,您想調用$(window).off('scroll')
。
$(window).unbind
是一種$(window).unbind
的舊方法。
邊注:
如果可以的話,您要避免放入內聯JS。
$(window).off('scroll')
應該可以解決問題。
$(window).unbind('scroll');
事件綁定到窗口 -執行代碼並綁定事件后, script
標記在頁面上的位置沒有變化。
實際上, unbind()
是老式的方法, $(window).off('scroll');
, $(window).off('scroll');
是“首選” -除非您被困在使用1.7之前的舊版jQuery。 unbind()
仍然有效,尚未被棄用,但總有可能。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.