jQuery:检查列表是否包含文本,然后重定向,否则重定向到此处

发布于 2024-12-08 13:19:00 字数 1025 浏览 0 评论 0原文

对于 jQuery,我是一个完全的新手,所以请耐心等待...

我有这个 HTML

<ul class="zoneSubscriptions">
  <li>
    <ul>
      <li class="zoneName"><a href="/Default.aspx?PageID=8267303">My Account</a></li>
      <li>Never</li>
    </ul>
  </li>
  <li>
    <ul>
      <li class="zoneName"><a href="/Default.aspx?PageID=8269026">Practitioners Area</a></li>
      <li>Never</li>
    </ul>
  </li>
</ul>

如果存在“实践者区域”的链接,请将浏览器重定向到该 href,否则,重定向到“我的帐户”部分。

这是我拥有的 jQuery...

jQuery.noConflict();
jQuery(document).ready(function() {

    if(jQuery(".zoneName a").text() == 'Practitioners Area'){
        document.location.href = $(this).attr('href');

    }else{
        document.location.href = jQuery('.zoneName:first a').attr('href');
    };


});

当它运行时,它只是将我重定向到“我的帐户”,即使存在“从业者区域”。

我知道我可能正在做一些非常愚蠢的事情......但是有人可以帮助我并给我带来光明吗?将不胜感激!谢谢!

I'm a complete novice when it comes to jQuery, so please bear with me...

I have this HTML

<ul class="zoneSubscriptions">
  <li>
    <ul>
      <li class="zoneName"><a href="/Default.aspx?PageID=8267303">My Account</a></li>
      <li>Never</li>
    </ul>
  </li>
  <li>
    <ul>
      <li class="zoneName"><a href="/Default.aspx?PageID=8269026">Practitioners Area</a></li>
      <li>Never</li>
    </ul>
  </li>
</ul>

If the link for Practitioners Area is present, redirect the browser to that href, else, redirect to the My Account section.

This is the jQuery I have...

jQuery.noConflict();
jQuery(document).ready(function() {

    if(jQuery(".zoneName a").text() == 'Practitioners Area'){
        document.location.href = $(this).attr('href');

    }else{
        document.location.href = jQuery('.zoneName:first a').attr('href');
    };


});

When it runs, it just redirects me to the My Account, even though the Practioners Area is present.

I know I'm probably doing something really stupid... But can anyone help me and show me the light? Would be much appreciated! Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

若沐 2024-12-15 13:19:00

if 语句中的条件更改为:

$(".zoneName a:contains('Practitioners Area')").length

您尝试的问题是 jQuery(".zoneName a").text() 将返回所有匹配的文本元素(在您的情况下,它返回类似“My AccountPractioners Area”的内容)。此版本检查是否有任何包含字符串“Practioners Area”的匹配元素。这是完整的代码:

jQuery(document).ready(function() {

    if($(".zoneName a:contains('Practitioners Area')").length) {
        document.location.href = $(this).attr('href');

    }else{
        document.location.href = jQuery('.zoneName:first a').attr('href');
    };
});

Change the condition in your if statement to this:

$(".zoneName a:contains('Practitioners Area')").length

The problem with your attempt is that jQuery(".zoneName a").text() will return the text of all matched elements (in your case, it returns something like "My AccountPractioners Area"). This version checks to see if there are any matching elements containing the string "Practioners Area". Here's the full code:

jQuery(document).ready(function() {

    if($(".zoneName a:contains('Practitioners Area')").length) {
        document.location.href = $(this).attr('href');

    }else{
        document.location.href = jQuery('.zoneName:first a').attr('href');
    };
});
江湖正好 2024-12-15 13:19:00

使用:

jQuery(document).ready(function() {
    var isPractitionersAreaPresent=false;
    jQuery(".zoneName a").text(function(i,v){
        if(v == 'Practitioners Area'){
            isPractitionersAreaPresent=true;
        }
    });
    if(isPractitionersAreaPresent){
        document.location.href = elem.attr('href');

    }else{
        document.location.href = jQuery('.zoneName:first a').attr('href');
    }
});

jQuery(".zoneName a").text() 返回".zoneName"中所有"a"元素的文本值

Use:

jQuery(document).ready(function() {
    var isPractitionersAreaPresent=false;
    jQuery(".zoneName a").text(function(i,v){
        if(v == 'Practitioners Area'){
            isPractitionersAreaPresent=true;
        }
    });
    if(isPractitionersAreaPresent){
        document.location.href = elem.attr('href');

    }else{
        document.location.href = jQuery('.zoneName:first a').attr('href');
    }
});

jQuery(".zoneName a").text() return the text value of all "a" elements in ".zoneName"

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