如果您对Javascript/jQueryonclick无法正常工作和jqueryclick事件无效感兴趣,那么这篇文章一定是您不可错过的。我们将详细讲解Javascript/jQueryonclic
如果您对Javascript / jQuery onclick无法正常工作和jqueryclick事件无效感兴趣,那么这篇文章一定是您不可错过的。我们将详细讲解Javascript / jQuery onclick无法正常工作的各种细节,并对jqueryclick事件无效进行深入的分析,此外还有关于javascript – $(document).click()在iPhone上无法正常工作. jquery [复制]、javascript – .slideToggle()在使用Jquery 3.1.1时无法正常工作、javascript – Android 4.2.2中的Jquery Mobile滑动事件无法正常工作、javascript – Concat JS无法正常工作的实用技巧。
本文目录一览:- Javascript / jQuery onclick无法正常工作(jqueryclick事件无效)
- javascript – $(document).click()在iPhone上无法正常工作. jquery [复制]
- javascript – .slideToggle()在使用Jquery 3.1.1时无法正常工作
- javascript – Android 4.2.2中的Jquery Mobile滑动事件无法正常工作
- javascript – Concat JS无法正常工作
Javascript / jQuery onclick无法正常工作(jqueryclick事件无效)
我做了一个test.html文档来测试脚本。不知何故它不起作用,我不明白为什么什么也没发生。该脚本位于-tags中,并带有-tag,而css也具有-
tags。为什么不起作用?这是代码
<!DOCTYPE html><html> <head> <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"> </script> <script> $(''#menu li a'').on(''click'', function() { $(''li a.current'').removeClass(''current''); $(this).addClass(''current''); }); </script> <style> .current { text-decoration: underline; } ul { list-style-type: none; } a { text-decoration: none; } </style> </head> <body> <div id="menu"> <ul> <li><a id="about-link"href="#">ABOUT</a></li> <li><a id="events-link" href="#">EVENTS</a></li> <li><a id="reviews-link" href="#">REVIEWS</a></li> <li><a id="contact-link" href="#">CONTACT</a></li> </ul> </div> </body></html>
答案1
小编典典由于用于绑定事件处理程序的代码是在元素存在于中之前执行的DOM
,因此事件未绑定到元素上。
要解决此问题,您可以
- 将您的代码包装在
ready()
回调中,以便DOM
在加载完成后执行您的代码 - 将您的脚本移动到的末尾
<body>
,以便所有元素都存在于其中DOM
,您可以在其上绑定事件
码:
$(document).ready(function () { $(''#menu li a'').on(''click'', function () { $(''li a.current'').removeClass(''current''); $(this).addClass(''current''); });});
编辑
您还可以使用load
( 不推荐使用 )事件回调来绑定事件,但这将等待所有资源完全加载。
如果您想使用Vanilla
Javascript,可以DOMContentLoaded
在上使用event document
。
javascript – $(document).click()在iPhone上无法正常工作. jquery [复制]
参见英文答案 > How do I use jQuery for click event in iPhone web application 8个
此功能在IE,Firefox和Chrome上完美运行,但在iPhone上,只有在点击< img>时才能使用.点击页面(在img上的任何地方)都不会触发事件.
$(document).ready(function () {
$(document).click(function (e) {
fire(e);
});
});
function fire(e) { alert('hi'); }
HTML部分非常基础,不应该是一个问题.
有任何想法吗?
解决方法:
简短回答:
<style>
.clickable-div
{
cursor: pointer;
}
</style>
更长的回答:
重要的是要意识到如果你只是使用< a>标签一切都会按预期工作.您可以在常规的< a>上错误地点击或拖动. iPhone上的链接,一切都像用户期望的那样.
我想你有任意不可点击的HTML – 例如包含文字和图像的面板,不能用< a>包装.当我有一个我希望完全可点击的面板时,我发现了这个问题.
<divdata-href="http://www.stackoverflow.com">
... clickable content here (images/text) ...
</div>
要检测此div中任何位置的单击,我将使用带有data-href html属性的jQuery,如上所示(此属性由我自己发明,不是标准的jQuery或HTML数据属性.)
$(document).on('click', '.clickable-div', function() {
document.location = $(this).data('href');
});
无论您点击多少,这都适用于您的桌面浏览器,但不适用于iPad.
您可能想要将事件处理程序从单击更改为单击touchstart – 这确实会触发事件处理程序.但是,如果用户想要向上拖动页面(滚动),它们也会触发它 – 这是一种糟糕的用户体验. [你可能已经注意到偷偷摸摸的横幅广告的这种行为]
答案非常简单:只需设置css游标:指针即可.
<style>
.clickable-div
{
cursor: pointer;
}
</style>
这为桌面用户提供了额外的好处,可以通过手形图标指示该区域是可点击的.
感谢https://stackoverflow.com/a/4910962/16940
javascript – .slideToggle()在使用Jquery 3.1.1时无法正常工作
jQuery(function($){ $(document).ready(function() { $(".block").click(function() { var currentElement = $(this).find(".form-group"); currentElement.slidetoggle("slow"); }); }); });
我收到以下错误
Uncaught TypeError: currentElement.slidetoggle is not a function at HTMLdivelement.<anonymous> (index.js:6) at HTMLdivelement.dispatch (jquery-3.1.1.slim.min.js:3) at HTMLdivelement.q.handle (jquery-3.1.1.slim.min.js:3)
但是,当我调用currentElement.toggle()它按预期工作,我没有得到任何错误.
谁能解释为什么会这样?
解决方法
从Jquery Page
Slim build
Sometimes you don’t need ajax,or you prefer to use one of the many standalone libraries that focus on ajax requests. And often it is simpler to use a combination of CSS and class manipulation for all your web animations. Along with the regular version of jQuery that includes the ajax and effects modules,we’ve released a “slim” version that excludes these modules. All in all,it excludes ajax,effects,and currently deprecated code.
javascript – Android 4.2.2中的Jquery Mobile滑动事件无法正常工作
我编写了一个使用JQuery移动swipeleft和swiperight事件的移动Web应用程序,但这些不适用于运行Android 4.2.2的三星galaxy S4.在标准Web浏览器或Chrome中运行Web应用程序具有相同的问题:未检测到滑动事件.
以下是我试图检测在我测试过的其他设备上工作正常的事件,甚至是Android设备(但不是Android 4.2.2):
$('#showImage').on("swipeleft", function (event) {
if (currentScale != initialScale) return;
if (currentPage < maxPage) {
++currentPage;
img.src = document.getElementById("page" + zeroPad(currentPage, 2) + "url").value;
}
});
$('#showImage').on("swiperight", function (event) {
if (currentScale != initialScale) return;
if (currentPage > 1) {
--currentPage;
img.src = document.getElementById("page" + zeroPad(currentPage, 2) + "url").value;
}
});
在Android 4.2.2中有什么我可以做的,代码方式来捕获这些事件吗?
解决方法:
你在这里有两个问题.
首先是由JQM中的一个已经解决但尚未实现的错误引起的
基本上,滑动事件测量的最小距离必须考虑设备的像素密度.所以在JQM的情况下,touch.js的以下更改将解决问题:
horizontaldistanceThreshold = window.devicePixelRatio >= 2 ? 15 : 30;
verticaldistanceThreshold = window.devicePixelRatio >= 2 ? 15 : 30;
第二个是todo与kitkat发射touchcancel而不是touchend.解决方案是将touchcancel委托给touchend.
javascript – Concat JS无法正常工作
var myjson = '{"name": "cluster","children": ['; for (var i = 0; i < unique.length; i++) { var uniquepart = '{"' + unique[i] + '"'; myjson.concat(uniquepart); var sizepart = ',"size:"'; myjson.concat(sizepart); var countpart = count[i] + ''; myjson.concat(countpart); if (i == unique.length) { myjson.concat(" },"); } else { myjson.concat(" }"); } } var ending = "]}"; myjson.concat(ending); console.log(myjson);
有谁知道为什么这个字符串没有正确连接,我仍然最终得到原始值?
解决方法
定义和用法
此方法不会更改现有字符串,但会返回包含已连接字符串文本的新字符串.
参考:http://www.w3schools.com/jsref/jsref_concat_string.asp
例如:
myjson = myjson.concat(uniquepart);
要么
myjson += uniquepart;
我们今天的关于Javascript / jQuery onclick无法正常工作和jqueryclick事件无效的分享就到这里,谢谢您的阅读,如果想了解更多关于javascript – $(document).click()在iPhone上无法正常工作. jquery [复制]、javascript – .slideToggle()在使用Jquery 3.1.1时无法正常工作、javascript – Android 4.2.2中的Jquery Mobile滑动事件无法正常工作、javascript – Concat JS无法正常工作的相关信息,可以在本站进行搜索。
本文标签: