jquery:将点击事件绑定到ajax加载的elmente? live() 不起作用?
嘿伙计们, 我有一个输入字段,用于在页面上查找匹配的字符。此页面仅列出锚链接。因此,当我打字时,我不断地 load() (使用 jquery load() 方法)这个包含所有链接的页面,并检查一组匹配的字符。如果找到匹配的链接,则会将其显示给用户。然而,所有这些链接都应该有 e.preventDefault() 。
它根本行不通。 #found 是显示匹配元素的容器。所有被点击的链接都应该有preventDefault()。
编辑:
/*Animated scroll for anchorlinks*/
var anchor = '',
pageOffset = '',
viewOffset = 30,
scrollPos = '';
$(function() {
$("a[href*='#']").each(function() {
$(this).addClass('anchorLink');
$(this).bind('click', function(e) {
e.preventDefault();
//console.log('test');
anchor = $(this).attr('href').split('#')[1];
pageOffset = $("#"+anchor).offset();
scrollPos = pageOffset.top - viewOffset;
$('html, body').animate({scrollTop:scrollPos}, '500');
})
});
});
嗯,我正在寻找所有包含 # 的 href。所以我知道这些元素是跳转到其他元素的锚点。我不希望页面跳转,而是平滑滚动到具有特定 #id 的该元素。
当我对打开页面时加载的普通页面元素使用 bind('click', ...
时,效果很好。它不适用于通过 ajax 加载的锚点如果我将 bind
更改为 live
,ajax 加载的元素不会发生任何变化 - 它们仍然不起作用,但是页面上一直存在的正常锚点却不起作用。所以 live() 也不起作用!
hey guys,
I have an input field that looks for matched characters on a page. This page simply lists anchor links. So when typing I constantly load() (using the jquery load() method) this page with all the links and I check for a matched set of characters. If a matched link is found it's displayed to the user. However all those links should have e.preventDefault() on them.
It simply won't work. #found is the container that shows the matched elements. All links that are clicked should have preventDefault() on them.
edit:
/*Animated scroll for anchorlinks*/
var anchor = '',
pageOffset = '',
viewOffset = 30,
scrollPos = '';
$(function() {
$("a[href*='#']").each(function() {
$(this).addClass('anchorLink');
$(this).bind('click', function(e) {
e.preventDefault();
//console.log('test');
anchor = $(this).attr('href').split('#')[1];
pageOffset = $("#"+anchor).offset();
scrollPos = pageOffset.top - viewOffset;
$('html, body').animate({scrollTop:scrollPos}, '500');
})
});
});
Well, I'm looking for all href's that contain a #. So I know those elements are anchors that jump to other elements. I don't want my page to jump, but rather scroll smoothly to this element with this specific #id.
This works fine when I use bind('click', ...
for normal page-elements that have been loaded when the page is opened. It doesn't work for anchors that have been loaded via ajax! If I change the bind
to live
nothing does change for the ajax loaded elements - they still don't work. However normal anchors that have always been on the page are not triggering the function as well. So nothing works with live()!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当你说“它不起作用”时,你的意思是你的函数没有被调用或者你不能取消该函数吗?据我所知,您无法取消现场活动。使用 jQuery 1.4,您可以使用 return false 来取消实时事件传播。调用 e.preventDefault() 不起作用。
编辑:正确,所以这段代码应该主要工作。它仍然不会做的是,它不会将“anchorLink”类添加到您的新锚点中。但是,如果点击有效,请告诉我,我也会为您提供正确的代码来添加该类!
When you say "it won't work" do you mean that your function is not been called or that you can not cancel out of the function? As far as I know you can not cancel out live events. With jQuery 1.4 you can use return false to cancel out live event propagation. Calling e.preventDefault() won't work.
Edit: right so this code should in principal work. What it still won't do is, it won't add the 'anchorLink' class to your new anchors. However if the clicks work then let me know and I will give you the right code to add the class too!