不能在 jquery 中使用 <a> 调用函数
Cannot call a function using <a> in jquery
<script type="text/javascript">
alert("hello");
$(function viewKart(event) {
alert("hello");
$("#popupdiv").dialog({
title: "AddCart",
width: 630,
height: 450,
modal: true,
buttons: {
Close: function () {
$(this).dialog('close')
}
}
});
return false;
});
</script>
<div class="text">
<a id="cartClick" onclick="viewKart();" href="#"> Cart(@if ( @App["CartLen"] != null) { @App["CartLen"] })</a>
</div>
<div id="popupdiv" title="Basic modal dialog" style="display: none">
<p>Hello World !</p>
</div>
我正在尝试调用 jquery 函数,但无法这样做。我有一个文本"Cart(0(",我想将其作为查看购物车的链接,这实际上是一个弹出窗口。
不需要 jQuery 环绕你的函数。
function viewKart() {
alert("hello");
$("#popupdiv").dialog({
title: "AddCart",
width: 630,
height: 450,
modal: true,
buttons: {
Close: function() {
$(this).dialog('close')
}
}
});
$("#popupdiv").dialog("open")
return false;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link rel="stylesheet" href="//ajax.googleapis.com/ajax/libs/jqueryui/1.11.1/themes/smoothness/jquery-ui.css" />
<script src="//ajax.googleapis.com/ajax/libs/jqueryui/1.11.1/jquery-ui.min.js"></script>
<div class="text">
<a id="cartClick" onclick="viewKart();" href="#"> Cart(...)</a>
</div>
<div id="popupdiv" title="Basic modal dialog" style="display: none">
<p>Hello World !</p>
</div>
使用 jQuery 处理程序的强大功能,并保持 html 干净:
1. 使用.on("click")
处理程序,而不是函数:
$(function(){
//alert("Document Ready");
$("#cartClick").on("click",function(e){
//alert("Button clicked!!");
$("#popupdiv").dialog({
title: "AddCart",
width: 630,
height: 450,
modal: true,
buttons: {
Close: function () {
$(this).dialog('close')
}
}
});
return false;
});
});
jQuery API : .on("click"(
2. 删除 HTML 上的onClick
属性。
<div class="text">
<a id="cartClick" href="#"> Cart(@if ( @App["CartLen"] != null) { @App["CartLen"] })</a>
</div>
每次用户单击ID为#cartClick
jQuery的元素时,都会触发您的函数(匿名(。
注意。我的回答仅供您了解,所有其他答案都同样有效。
函数viewKart
不在全局范围内
alert("hello");
$(viewKart);
function viewKart(event) {
alert("hello");
$("#popupdiv").dialog({
title: "AddCart",
width: 630,
height: 450,
modal: true,
buttons: {
Close: function () {
$(this).dialog('close')
}
}
});
return false;
}
相关文章:
- 如何从模块链中调用函数.导出到节点中
- 调用函数内部的函数
- 在javascript中调用c函数
- DropDownListFor赢得't在更改时调用函数
- Javascript页面调用函数
- 在动画结束之前调用函数
- 允许父窗口在其不同域的子iframe上调用函数
- 运行Infinite Scroll后调用函数时出现问题
- JavaScript:在调用函数的文本输入上按enter键
- 使用大括号和不使用bracs调用函数的区别
- javascript在事件上调用函数
- 从index.html调用函数,该函数无限循环
- 从带参数的字符串变量调用函数中的函数
- 为什么 JS 不在滚动时调用函数
- 是否可以在不更改上下文的情况下调用函数.apply
- 如何在ES6类中使用参数调用函数
- AngularJS:调用函数时编号不更新
- JavaScript中的立即调用函数表达式(IIFE)-传递jQuery
- 在JavaScript中调用函数时自定义此选项
- 调用函数中声明的变量