Jquery Live点击功能不起作用
我的 Jquery live 功能有问题。我正在动态添加数据,因此我必须使用实时功能才能单击动态添加的元素。
$("a.pm_member").live("click", function(){
alert('clicked');
});
数据已正确添加。没有错误消息,但不显示警报窗口。
如果有人能帮助我解决这个问题,我会很高兴。
编辑:
我想提供更多详细信息。
这是我在页面中使用的;
<script>
new TINY.editor.edit('editor',{
id:'tiny_input',
height:200,
cssclass:'te',
controlclass:'tecontrol',
rowclass:'teheader',
dividerclass:'tedivider',
controls:['bold','italic','underline','strikethrough','|','subscript','superscript','|',
'orderedlist','unorderedlist','|','outdent','indent','|','leftalign',
'centeralign','rightalign','blockjustify','|','unformat','|','undo','redo','n','image','hr','link','unlink','|','cut','copy','paste','print','|','font','size','style'],
footer:false,
fonts:['Arial','Verdana','Georgia','Trebuchet MS'],
xhtml:true,
cssfile:'style.css',
bodyid:'editor',
footerclass:'tefooter',
toggle:{text:'source',activetext:'wysiwyg',cssclass:'toggler'},
resize:{cssclass:'resize'}
});
$('button#post').click(function () {
editor.post();
});
$('#suggestions').hide();
function hideSuggestions () {
$('#suggestions').hide();
}
$("a.pm_member").live("click", function(){
alert('clicked');
});
</script>
这是一个PM系统。当管理员在接收者输入区域写入用户名时,脚本会建议一些用户名。
<input type="text" name="userName" class="medium" maxlength="100" onkeyup="findMember(this.value);" onblur="hideSuggestions();" onfocus="findMember(this.value);"/>
<div id="suggestions"></div>
index.php 中包含超过 1 页。我可以在任何其他页面中使用 .live() 函数,没有任何问题,但在 send-pm.php 中则不行。
我很困惑如何调试它以及如何解决它。
.bind()、.live()、.find() 它就是不起作用。
以下元素是动态添加的;
<div class="flat_area grid_8 searchresults">
<h2 class="box_head grad_colour round_top">Member Suggestions</h2>
<div class="block">
<ul class="full_width">
<li>
<strong>Username: </strong> <a href="#" id="{$found->id}" class="pm_member">{$found->username}</a><br />
<strong>Full Name: </strong> {$found->fullName}<br />
<strong>Email: </strong>{$email}
</li>
<li>Total <strong>{$totalRecords}</strong> records found.</li>
</ul>
</div>
</div>
有什么想法如何调试它吗?
编辑2:
我什至尝试在添加 jquery 和 jquery ui 文件后立即在元素之间添加以下代码。
<!-- Load JQuery -->
<script type="text/javascript" src="includes/js/jquery-1.5.1.min.js"></script>
<!-- Load JQuery UI -->
<script type="text/javascript" src="includes/js/jquery-ui-1.8.11.custom.min.js"></script>
<script>
$(document).ready(function() {
$("a.pm_member").live("click", function(){
alert('clicked');
});
});
</script>
如果这是关于一些 Jquery 代码制动它,不应该修复它(虽然我对 Jquery 不太擅长)。
编辑3:
我寻找会员的功能
function findMember (inputString) {
if(inputString.length == 0) {
$('#suggestions').fadeOut(); // Hide the suggestions box
} else {
$.get(siteUrl + "includes/ajax/profile.php?do=find-member", {username: ""+inputString+""}, function(data) { // Do an AJAX call
$('#suggestions').fadeIn(); // Show the suggestions box
$('#suggestions').html(data); // Fill the suggestions box
});
}
}
再次感谢您的帮助和关心。
I have got a problem with Jquery live function. I am adding data dynamically so I have to use live function in order to get clicked a element which added dynamically.
$("a.pm_member").live("click", function(){
alert('clicked');
});
Data is added correctly. There are no error messages but it doesn't display alert window.
I will be glad if anyone could help me out with this problem.
Edit:
I would like to give more details.
This is what I use in the page;
<script>
new TINY.editor.edit('editor',{
id:'tiny_input',
height:200,
cssclass:'te',
controlclass:'tecontrol',
rowclass:'teheader',
dividerclass:'tedivider',
controls:['bold','italic','underline','strikethrough','|','subscript','superscript','|',
'orderedlist','unorderedlist','|','outdent','indent','|','leftalign',
'centeralign','rightalign','blockjustify','|','unformat','|','undo','redo','n','image','hr','link','unlink','|','cut','copy','paste','print','|','font','size','style'],
footer:false,
fonts:['Arial','Verdana','Georgia','Trebuchet MS'],
xhtml:true,
cssfile:'style.css',
bodyid:'editor',
footerclass:'tefooter',
toggle:{text:'source',activetext:'wysiwyg',cssclass:'toggler'},
resize:{cssclass:'resize'}
});
$('button#post').click(function () {
editor.post();
});
$('#suggestions').hide();
function hideSuggestions () {
$('#suggestions').hide();
}
$("a.pm_member").live("click", function(){
alert('clicked');
});
</script>
This is a PM system. When admin writes a username in receiver input area, script is suggesting some usernames.
<input type="text" name="userName" class="medium" maxlength="100" onkeyup="findMember(this.value);" onblur="hideSuggestions();" onfocus="findMember(this.value);"/>
<div id="suggestions"></div>
There are more than 1 page included in index.php. I can use .live() function without any problem in any other page but not in send-pm.php.
I am quite confused how to debug it and how to solve it.
.bind(), .live(), .find() it just doesn't work.
Following elements are dynamically added;
<div class="flat_area grid_8 searchresults">
<h2 class="box_head grad_colour round_top">Member Suggestions</h2>
<div class="block">
<ul class="full_width">
<li>
<strong>Username: </strong> <a href="#" id="{$found->id}" class="pm_member">{$found->username}</a><br />
<strong>Full Name: </strong> {$found->fullName}<br />
<strong>Email: </strong>{$email}
</li>
<li>Total <strong>{$totalRecords}</strong> records found.</li>
</ul>
</div>
</div>
Any ideas how to debug it?
EDIT 2:
I even tried to add following code between element just right after adding jquery and jquery ui files.
<!-- Load JQuery -->
<script type="text/javascript" src="includes/js/jquery-1.5.1.min.js"></script>
<!-- Load JQuery UI -->
<script type="text/javascript" src="includes/js/jquery-ui-1.8.11.custom.min.js"></script>
<script>
$(document).ready(function() {
$("a.pm_member").live("click", function(){
alert('clicked');
});
});
</script>
If this would be about some Jquery code braking it, shouldn't that fix it (I am not that good with Jquery though).
EDIT 3:
My function to find members
function findMember (inputString) {
if(inputString.length == 0) {
$('#suggestions').fadeOut(); // Hide the suggestions box
} else {
$.get(siteUrl + "includes/ajax/profile.php?do=find-member", {username: ""+inputString+""}, function(data) { // Do an AJAX call
$('#suggestions').fadeIn(); // Show the suggestions box
$('#suggestions').html(data); // Fill the suggestions box
});
}
}
Once again, thank you for your help and concern.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您的
“a.pm_member”
元素的任何祖先上有click
处理程序,并且返回false;
或者event.stopPropagation()
,它会终止.live()
。这是因为
.live()
完全依赖于事件委托。这意味着只有那些成功从单击的元素冒泡到
document
的事件才能被.live()
分析并可能被调用。如果事件被某个祖先停止,
.live()
处理程序将永远不会看到该事件。编辑:
这是分配了防止冒泡的处理程序的祖先的示例。
示例: http://jsfiddle.net/aBtDJ/
删除阻止冒泡的处理程序会导致
.live()
处理程序工作一次再次。示例: http://jsfiddle.net/aBtDJ/1 /
编辑:
具体问题是
元素上有一个内联
onblur="hideSuggestions()"
就在你之前#suggestions
元素。这似乎用于在您单击任意位置时删除对话框。因此,
blur
事件发生在click
之前,因此它永远不会注册。这个问题没有完美的解决方案。无论如何,模糊都会在点击之前发生。
一种可能是使用
mousedown
事件,该事件应该在blur
之前发生。当然,这不是完整的点击,但可能已经足够好了。我个人会使用
.delegate()
而不是.live()
,但两者都可以。下面是一个.delegate()
示例:另一种可能性是将您的代码分配给
suggestions
元素之前的同一输入元素。当然,这会在您点击的任何地方触发,因此它可能不是您想要的。If you have
click
handlers on any ancestors of your"a.pm_member"
element that doreturn false;
orevent.stopPropagation()
, it will kill.live()
.This is because
.live()
relies entirely on event delegation.This means that only those events that successfully bubble from the element clicked, up to the
document
, can be analyzed by.live()
and potentially invoked.If the event is halted by some ancestor, the
.live()
handler will never see the event.EDIT:
This is an example of an ancestor with a handler assigned that prevents bubbling.
Example: http://jsfiddle.net/aBtDJ/
Removing the handler that blocked the bubbling causes the
.live()
handler to work once again.Example: http://jsfiddle.net/aBtDJ/1/
EDIT:
The specific issue is that there's an inline
onblur="hideSuggestions()"
on the<input>
element just before your#suggestions
element.This appears to be used to remove the dialog when you click anywhere. As such, the
blur
event occurs before theclick
, so it never registers.There isn't a perfect solution for this problem. No matter what, the
blur
is going to happen before the click.One possibility would be to use the
mousedown
event, which should happen before theblur
. Of course, this isn't a full click, but it may be good enough.I'd personally use
.delegate()
instead of.live()
, but either would work. Here's a.delegate()
example:Another possibility is to assign your code to that same input element before the
suggestions
element. Of course this would trigger anywhere you click, so it may not be what you want.尝试使用bind()而不是live(),
如果这不起作用,我建议使用 find()
示例
如果parentClass是类pm_member的anchor的父类,
则使用它
作为find()的参考--> http://api.jquery.com/find/
供参考bind() --> http://api.jquery.com/bind/
try using bind() instead of live(),
if this doesn't work I will suggest using find()
example
if parentClass is a parent class of anchor with class pm_member
use this
for reference on find()--> http://api.jquery.com/find/
for reference on bind() --> http://api.jquery.com/bind/