为什么jQuery版本1.4.2中没有$?

时间:2017-03-01 20:16:15

标签: javascript jquery html css

这是我的代码:

$("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的情况下使代码在上面工作?

2 个答案:

答案 0 :(得分:3)

在jQuery 1.7版本中添加了jQuery .on()函数。

您应该使用jQuery 1.0版中添加的.click()函数。请注意,在1.4.3版之前,您无法使用此函数指定eventData。

对于除click之外的事件,有类似的函数,所有这些函数都存在于版本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>

答案 1 :(得分:2)

$被认可了。 由于错误说问题是该版本上不存在.on方法。

直接使用click

$("span.cls").click(function(){
  $("#dialog").html("");
});

甚至更好(应优先)升级您的版本( 1.4.2于2010年2月发布)。