JQuery 的前缀选择器问题

发布于 2024-11-05 19:47:19 字数 2617 浏览 0 评论 0原文

我有这些 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

木槿暧夏七纪年 2024-11-12 19:47:19

我强烈建议您不要从属性的一部分中进行选择。添加特定于每个链接的类,例如 link-enabledlink-disablelink-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 and link-delete.

Then you'll can select like $(".link-enabled").click() wich causes less problems and also has better performance.

荆棘i 2024-11-12 19:47:19

我认为使用标签作为选择器是一个坏主意。您可以轻松地将类添加到所有相关元素并将其用作选择器。除此之外,我认为通过使用函数你的代码可以更短。

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.

怎樣才叫好 2024-11-12 19:47:19

您的头衔可能会随着时间的推移而改变。我会这样做:

$('a[title]').click(function(){
  if($(this).closest("li").hasClass("iconDelete")){
    alert(1);
    //put the rest of your code
  }
  if($(this).closest("li").hasClass("iconActive1")){
    alert(2);
    //put the rest of your code
  }
  if($(this).closest("li").hasClass("iconActive0")){
    alert(2);
    //put the rest of your code
  }
});

当然,您可以将函数中的代码分开以获得更好的可读性。

希望这有帮助。干杯

PS:我建议您将不同的类放入 a 元素并按类选择它们,这是一种更“正确”的方法。

Your title may change over time. I'd do this:

$('a[title]').click(function(){
  if($(this).closest("li").hasClass("iconDelete")){
    alert(1);
    //put the rest of your code
  }
  if($(this).closest("li").hasClass("iconActive1")){
    alert(2);
    //put the rest of your code
  }
  if($(this).closest("li").hasClass("iconActive0")){
    alert(2);
    //put the rest of your code
  }
});

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.

源来凯始玺欢你 2024-11-12 19:47:19

这里有另一个完整的解决方案:

<li class="icon iconDelete"><a href="/projects/delete/{id}/" class="delete" title="Delete this test case."></a></li>
<li class="icon iconActive1"><a href="/projects/disable/{id}/" class="disable" title="Disable this test case."></a></li>
<li class="icon iconActive0"><a href="/projects/enable/{id}/" class="enable" title="Enable this test case."></a></li>

function doAction(action){
   alert(action);
   $('#overlayTitle').text('Are you sure you want to '+action+' 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);
    });
}

$(document).ready(function(){
   $('a.enable').click(function(){
      doAction('enable');   
   });
   $('a.disable').click(function(){
      doAction('disable');   
   });
   $('a.delete').click(function(){
      doAction('delete');   
   });
});

希望这会有所帮助。干杯

Here you have another complete solution:

<li class="icon iconDelete"><a href="/projects/delete/{id}/" class="delete" title="Delete this test case."></a></li>
<li class="icon iconActive1"><a href="/projects/disable/{id}/" class="disable" title="Disable this test case."></a></li>
<li class="icon iconActive0"><a href="/projects/enable/{id}/" class="enable" title="Enable this test case."></a></li>

function doAction(action){
   alert(action);
   $('#overlayTitle').text('Are you sure you want to '+action+' 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);
    });
}

$(document).ready(function(){
   $('a.enable').click(function(){
      doAction('enable');   
   });
   $('a.disable').click(function(){
      doAction('disable');   
   });
   $('a.delete').click(function(){
      doAction('delete');   
   });
});

Hope this helps. Cheers

删除会话 2024-11-12 19:47:19

发生这种情况是因为您使用了错误的选择器。

属性包含前缀选择器 [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 (-)

Description: Selects elements that have the specified attribute with a
value either equal to a given string
or starting with that string followed
by a hyphen (-)
.

You should be using the Attribute Starts With Selector [name^="value"] that works
like this

Description: Selects elements that have the specified attribute with a
value beginning exactly with a given
string.

You should also call the event.preventDefault() method so the links do not go through..

demo http://jsfiddle.net/gaby/BdyPB/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文