jQuery:检查列表是否包含文本,然后重定向,否则重定向到此处
对于 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
将
if
语句中的条件更改为:您尝试的问题是
jQuery(".zoneName a").text()
将返回所有匹配的文本元素(在您的情况下,它返回类似“My AccountPractioners Area”的内容)。此版本检查是否有任何包含字符串“Practioners Area”的匹配元素。这是完整的代码:Change the condition in your
if
statement to this: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(".zoneName a").text() 返回".zoneName"中所有"a"元素的文本值
Use:
jQuery(".zoneName a").text() return the text value of all "a" elements in ".zoneName"