[英].find doesn't work in jquery 1.4.2 and I can't figure out how to support it
[英]Why doesn't $ recognized in jQuery version 1.4.2?
這是我的代碼:
$("span.cls").on("click", function(){ $("#dialog").html(""); });
span{ border: 1px solid; cursor: pointer; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script> <span class="cls">remove content below</span> <div id="dialog"><p>something</p></div>
是否可以在不更改同一版本的jQuery的情況下使上述代碼正常工作?
jQuery .on()
函數是在jQuery 1.7版中添加的。
您應該改用.click()
函數,該函數是jQuery 1.0版中添加的。 請注意,在1.4.3版之前,不能使用此功能指定eventData。
對於單擊以外的事件,有類似的功能,所有這些功能都從1.0版開始存在:
$("span.cls").click(function(){ $("#dialog").html(""); });
span{ border: 1px solid; cursor: pointer; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script> <span class="cls">remove content below</span> <div id="dialog"><p>something</p></div>
您還可以選擇使用.live()
通用事件綁定程序,該事件綁定程序將事件類型指定為字符串,就像.on()
函數一樣。 在jQuery 1.7版中,不贊成使用.live()
函數,而建議使用.on()
,但是由於您使用的是jQuery的較舊版本,因此仍然合適。
$("span.cls").live("click", function(){ $("#dialog").html(""); });
span{ border: 1px solid; cursor: pointer; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script> <span class="cls">remove content below</span> <div id="dialog"><p>something</p></div>
$
被認為很好。 如錯誤所示,問題是該.on
上不存在.on
方法。
直接使用click
$("span.cls").click(function(){
$("#dialog").html("");
});
甚至更好( 實際上應該是一個優先事項 )升級您的版本( 因為1.4.2於2010年2月發布 )。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.