我有一些代码,我循环遍历页面上的所有选择框,并将.hover
事件绑定到它们,以便在它们的宽度上做一些麻烦mouse on/off
.
这发生在页面准备就绪并且工作得很好.
我遇到的问题是,我在初始循环后通过Ajax或DOM添加的任何选择框都不会受到事件限制.
我找到了这个插件(jQuery Live Query Plugin),但在我用插件添加另外5k到我的页面之前,我想知道是否有人知道这样做的方法,无论是直接使用jQuery还是通过其他选项.
我正在尝试使用:
$('mydiv').delegate('hover', function() {
$('seconddiv').show();
}, function() {
//For some reason jQuery won't run this line of code
$('seconddiv').hide();
});
Run Code Online (Sandbox Code Playgroud) 在过去,我曾经以live()
极大的成功连锁电话,例如:
$(".something")
.live("click", function(e) { ... })
.live("change", function(e) { ... })
.live("submit", function(e) { ... });
Run Code Online (Sandbox Code Playgroud)
这些天来,live()
,bind()
并delegate()
已被崭新的取代on()
.
我试着简单地更换live()
与on()
这似乎显而易见:
$(".something")
.on("click", function(e) { ... })
.on("change", function(e) { ... })
.on("submit", function(e) { ... });
Run Code Online (Sandbox Code Playgroud)
然而,当您考虑如何工作时,这几乎是显而易见的on()
.这来自http://api.jquery.com/on/:
"事件处理程序仅绑定到当前选定的元素;它们必须存在于代码调用.on()时页面上."
根据jQuery文档,我需要绑定document
并委托.something
处理实时事件.不幸的是,这意味着.document
如果我想复制上面的内容,我最终会重复我的委托选择器()on()
:
$(document)
.on("click", ".something", function(e) { ... })
.on("change", ".something", function(e) { ... })
.on("submit", ".something", function(e) …
Run Code Online (Sandbox Code Playgroud) 我很生气 - 也许有人能够帮助我.
我需要在AJAX调用之后将点击重新绑定到链接,但由于某种原因它不想工作.
这是我的代码:
if ($('.active').length > 0) {
$('.active').click(function() {
var elem = $(this);
var url = $(this).attr('href');
$.ajax({
url: url,
dataType: 'html',
success: function(data) {
elem.replaceWith(data);
}
});
$('.active').bind('click'); return false;
});
}
Run Code Online (Sandbox Code Playgroud)
任何的想法?
感谢您的回复 - 我修改了代码,但问题仍然存在:
function makeActive() {
if ($('.active').length > 0) {
$('.active').click(function() {
var elem = $(this);
var url = $(this).attr('href');
$.ajax({
url: url,
dataType: 'html',
success: function(data) {
elem.replaceWith(data);
}
});
$('.active').live('click', makeActive);
return false;
});
}
}
$('.active').live('click', makeActive);
Run Code Online (Sandbox Code Playgroud) 我用jQuery创建了两个元素:
图片和关闭按钮
我写了代码来删除doument.ready函数:
$( ".deletepreview" ).click(function() {
code = $(this).data("prevcode");
$('#'+code).remove();
$(this).remove();
});
Run Code Online (Sandbox Code Playgroud)
但它不起作用,我认为这是因为代码不会搜索文档加载后创建的代码.
我怎么解决这个问题?
假设我有一个链接:
<a href='http://example.com/file.zip' id='dl'>Download</a>
Run Code Online (Sandbox Code Playgroud)
当有人点击它时,我想做点什么,比如:
$("#dl").live("click", function(e){
e.preventDefault();
$("#dlbox").fadeOut(500);
});
Run Code Online (Sandbox Code Playgroud)
在我e.preventDefault之后有没有办法,继续doDefault?就像是
$("#dl").live("click", function(e){
e.preventDefault();
$("#dlbox").fadeOut(500);
e.doDefault();
});
Run Code Online (Sandbox Code Playgroud)
所以它会正常运行事件?
我知道"实时"功能现已弃用.如何迁移以下内容以使用新的"on"?
$('a.action').live( "click", function (evt) {
// Do stuff
}
Run Code Online (Sandbox Code Playgroud)
场景是a.action正在动态创建.我试过这个,但无济于事:
$('a.action').on( "click", function (evt) {
// Do stuff
}
Run Code Online (Sandbox Code Playgroud) 我有两个jquery函数一起工作,一个依赖于一个类,另一个取消了类.
一旦它被删除,我希望功能停止工作,但它继续吗?
这是怎么回事?
这是小提琴,为自己试试.
<div class="container disabled">
<a href="www.google.com">Go to Google</a>
</div>
<label>
<input type="checkbox" />Enable link</label>
Run Code Online (Sandbox Code Playgroud)
JS
$('.disabled > a').click(function (e) {
e.preventDefault();
e.stopPropagation();
alert('should stop working');
});
$('input[type=checkbox]').change(function () {
$('.container').removeClass('disabled');
});
Run Code Online (Sandbox Code Playgroud) 很多人都解释说e.stopPropagation()
可以防止事件冒泡.但是,我很难找到为什么人们想要或者想要首先阻止事件冒泡.
在我的网站上,我有许多元素,如下所示:
$(document.body).on('click', ".clickable", function(e){
//e.stopPropagation();
//do something, for example show a pop-up or click a link
});
<body>
<p>outside stuff</p>
<button type="button" class='clickable'>
<img src='/icon.jpg'> Do Something
</button>
</body>
Run Code Online (Sandbox Code Playgroud)
我想添加e.stopPropagation()
因为我想要将事件处理程序更改'touch'
为'click'
使用这个真棒触摸库Hammer.js..这将允许点击在桌面上正常发生以及在移动设备上进行触摸事件.
这个问题(请纠正我,如果我错了)是触摸设备上的滚动减慢停止.
这e.stopPropgation()
有用吗?这样,无论何时触摸屏幕 - document.body
事件冒泡都不会每次都发生?
javascript jquery javascript-events event-bubbling stoppropagation
我很好奇为什么当我.live()
用.on()
我的事件替换后,通过html()
方法插入AJAX的响应后不起作用.假设我有html结构:
<div class="a">
<a href="" class="alert-link">alert</a>
<a href="" class="ajax-update">update</a>
</div>
Run Code Online (Sandbox Code Playgroud)
和jquery代码类似:
$('.alert-link').on("click", function(){
alert('abc');
return false;
});
Run Code Online (Sandbox Code Playgroud)
和ajax-update将触发请求,响应将是
警报更新
我会插入它parent()
.然后再次按下alert-link
将导致重定向到/
但如果我改为.on()
,.live()
那么将再次显示警报.我在这里错过了什么?我已经读到这.on()
是替换.delegate()
和.live()
.
我在我的页面上使用以下代码,我在 ajax 中加载。
$(document).ready(function() {
$('#button_id').click(function() {
//Do Something
});
});
Run Code Online (Sandbox Code Playgroud)
现在当我点击按钮动作发生多次。我知道这是因为我多次加载 ajax 页面。
请帮我解决这个问题。
我有以下jQuery
$("button#submit").click(function(){
$.ajax({
type: "POST",
url: "process.php",
data: $('form.contact').serialize(),
success: function(msg){
$("#form-content").modal('hide');
$("#thanks").html(msg);
$("#thanks").delay(2000).fadeOut("slow");
},
error: function(){
alert("failure");
}
});
});
Run Code Online (Sandbox Code Playgroud)
和Php
<?php
if (isset($_POST['name'])) {
$name = strip_tags($_POST['name']);
$email = strip_tags($_POST['Email']);
$sug = strip_tags($_POST['sug']);
echo "<span class='label label-info'>Your Website has been submitted .. Thank you</span>";
}?>
Run Code Online (Sandbox Code Playgroud)
这是第一次工作,并在我的页面上显示php echo.但是,当我再次提交表格时,它没有显示.