JQuery 的前缀选择器问题
我有这些 HTML 片段:
<li class="icon iconDelete"><a href="/projects/delete/{id}/" title="Delete this test case."></a></li>
<li class="icon iconActive1"><a href="/projects/disable/{id}/" title="Disable this test case."></a></li>
<li class="icon iconActive0"><a href="/projects/enable/{id}/" title="Enable this test case."></a></li>
我
<div class="overlay" id="overlay" style="display:none;"></div>
<ul class="overlayBox" id="overlayBox">
<li id="overlayTop"></li>
<li id="overlayContent">
<b id="overlayTitle">Title</b><br />
<p id="overlayText">
Here comes a very important message for your user.
Turn this window off by clicking the cross.
</p>
</li>
<li id="overlayBottom"></li>
</ul>
有这段 JQuery:
$(document).ready(
$(function()
{
$('a[title|="Enable this "]').click(
function()
{
alert(1);
$('#overlayTitle').text('Are you sure you want to enable this item?');
$('#overlayText').text('This will take effect for all parent items this item is assigned to.');
$('#overlay').fadeIn('fast',function()
{
$('#overlayBox').animate({'top':'160px'},500);
});
}
),
$('a[title|="Disable this "]').click(
function()
{
alert(2);
$('#overlayTitle').text('Are you sure you want to disable this item?');
$('#overlayText').text('This will take effect for all parent items this item is assigned to.');
$('#overlay').fadeIn('fast',function()
{
$('#overlayBox').animate({'top':'160px'},500);
});
}
),
$('a[title|="Delete this "]').click(
function()
{
alert(3);
$('#overlayTitle').text('Are you sure you want to delete this item?');
$('#overlayText').text('This will take effect for all parent items this item is assigned to.');
$('#overlay').fadeIn('fast',function()
{
$('#overlayBox').animate({'top':'160px'},500);
});
}
);
}
)
);
当我单击带有 title 属性的链接之一时,jQuery 会进入所有 .click() 函数(将弹出 3 个警报),而不仅仅是用于指定的前缀选择器。
知道为什么以及如何解决这个问题吗?
谢谢!
I have these pieces of HTML:
<li class="icon iconDelete"><a href="/projects/delete/{id}/" title="Delete this test case."></a></li>
<li class="icon iconActive1"><a href="/projects/disable/{id}/" title="Disable this test case."></a></li>
<li class="icon iconActive0"><a href="/projects/enable/{id}/" title="Enable this test case."></a></li>
and
<div class="overlay" id="overlay" style="display:none;"></div>
<ul class="overlayBox" id="overlayBox">
<li id="overlayTop"></li>
<li id="overlayContent">
<b id="overlayTitle">Title</b><br />
<p id="overlayText">
Here comes a very important message for your user.
Turn this window off by clicking the cross.
</p>
</li>
<li id="overlayBottom"></li>
</ul>
I have this piece of JQuery:
$(document).ready(
$(function()
{
$('a[title|="Enable this "]').click(
function()
{
alert(1);
$('#overlayTitle').text('Are you sure you want to enable this item?');
$('#overlayText').text('This will take effect for all parent items this item is assigned to.');
$('#overlay').fadeIn('fast',function()
{
$('#overlayBox').animate({'top':'160px'},500);
});
}
),
$('a[title|="Disable this "]').click(
function()
{
alert(2);
$('#overlayTitle').text('Are you sure you want to disable this item?');
$('#overlayText').text('This will take effect for all parent items this item is assigned to.');
$('#overlay').fadeIn('fast',function()
{
$('#overlayBox').animate({'top':'160px'},500);
});
}
),
$('a[title|="Delete this "]').click(
function()
{
alert(3);
$('#overlayTitle').text('Are you sure you want to delete this item?');
$('#overlayText').text('This will take effect for all parent items this item is assigned to.');
$('#overlay').fadeIn('fast',function()
{
$('#overlayBox').animate({'top':'160px'},500);
});
}
);
}
)
);
When I click on one of the link with the title attribute, jQuery goes in all .click() functions (3 alerts will pop up), not only the one for the specified prefix selector.
Any idea why and how to fix this?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我强烈建议您不要从属性的一部分中进行选择。添加特定于每个链接的类,例如
link-enabled
、link-disable
和link-delete
。然后您可以选择像
$(".link-enabled").click()
这样会导致更少的问题并且具有更好的性能。I strongly suggest you not select from part of an attribute. Add classes specific to each link, like
link-enabled
,link-disable
andlink-delete
.Then you'll can select like
$(".link-enabled").click()
wich causes less problems and also has better performance.我认为使用标签作为选择器是一个坏主意。您可以轻松地将类添加到所有相关元素并将其用作选择器。除此之外,我认为通过使用函数你的代码可以更短。
I think it's a bad idea to use the label as selector. You can easily add a class to all related elements and use it as selector. Besides that, I think your code could be shorter by using a function.
您的头衔可能会随着时间的推移而改变。我会这样做:
当然,您可以将函数中的代码分开以获得更好的可读性。
希望这有帮助。干杯
PS:我建议您将不同的类放入
a
元素并按类选择它们,这是一种更“正确”的方法。Your title may change over time. I'd do this:
Of course you can separate the code in functions for better readability.
Hope this helps. Cheers
PS: I'd recommend you to put different clases to the
a
elements and select them by class, it's a more 'correct' way.这里有另一个完整的解决方案:
希望这会有所帮助。干杯
Here you have another complete solution:
Hope this helps. Cheers
发生这种情况是因为您使用了错误的选择器。
在
属性包含前缀选择器 [name|="value"]
(您使用的)该值后面需要跟一个连字符 (-)您应该使用
Attribute Starts With Selector [name^="value"]
有效像这样
您还应该调用
event.preventDefault()
方法,以便链接不会通过。.演示 http://jsfiddle.net/gaby/BdyPB/
This happens because you are using the wrong selector.
In the
Attribute Contains Prefix Selector [name|="value"]
(that you use) the value needs to be followed by a hyphen (-)You should be using the
Attribute Starts With Selector [name^="value"]
that workslike this
You should also call the
event.preventDefault()
method so the links do not go through..demo http://jsfiddle.net/gaby/BdyPB/