[英]Calling a Script when button clicked
In a jquery mobile webpage, I would like to call a script defined within script tags in the HEAD section of the jsp. 在jquery移动网页中,我想调用jsp的HEAD部分中的脚本标签中定义的脚本。
<head>
<link rel="stylesheet" type="text/css" href="jquerymobile/jquery.mobile-1.0a1.min.css" />
<script type="text/javascript" src="jquerymobile/jquery-1.4.3.min.js"></script>
<script type="text/javascript" src="jquerymobile/jquery.mobile-1.0a1.min.js"></script>
<script>
$("#button").click( function()
{
alert('button clicked');
}
);
</script>
</head>
<body>
<html:errors/>
<div data-role="page" data-theme='a'>
<div data-role="header">
<h1>jquerymobile</h1>
</div>
<div data-role="content">
<div id="button" data-role="button">Click on button</div>
</div>
</div>
</body>
When the Button is clicked , I want the alert to be shown . 单击按钮时 ,我希望显示警报 。 But nothing happens when the button is clicked.
但是,单击按钮后什么也没有发生。
Could someone tell me where I am going wrong?
有人可以告诉我我要去哪里了吗?
EDIT1: EDIT1:
The following code brings up the alert in Firefox & Opera Desktop Browsers . 以下代码在Firefox和Opera桌面浏览器中 弹出警报 。
$(document).ready(function() {
$("#vbutton").click(function() {
alert("clicked");
});
});
<a id="vbutton" data-role="button">Click Button</a>
Any reason why the same does not work with Opera Mobile & Fennec browsers - where it shows the Error Loading Page dialog?? 是什么原因导致同一操作不适用于Opera Mobile和Fennec浏览器 -显示“ 错误加载页面”对话框的地方?
Your problem is that you are searching for $('#button')
before it exists... 您的问题是您正在搜索
$('#button')
之前...
Try wrapping your jQuery code in a $(document).ready(function(){ ... })
or its shorthand alias jQuery(function(){})
尝试将jQuery代码包装在
$(document).ready(function(){ ... })
或其速记别名jQuery(function(){})
The other option is to include your initialization of the click event on the button until AFTER the #button
exists in the DOM. 另一个选择是在按钮中存在
#button
之后,在DOM中包含按钮事件的初始化。 In other words, you can move your <script>
tag to the bottom of the page and it should work. 换句话说,您可以将
<script>
标记移动到页面底部,它应该可以工作。
The problem was due to debug statements like 问题是由于调试语句如
debugger;
调试器;
debug.info(....)
调试信息(....)
which were understood by Firefox/Firebug, but were causing problems in Fennec & Opera Mobile browsers . Firefox / Firebug可以理解,但在Fennec&Opera Mobile浏览器中会引起问题 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.