简体   繁体   English

试图通过jQuery / replace()删除括号内的锚文本

[英]Trying to remove anchor text within brackets via jQuery / replace()

I've been searching through stackoverflow to find a solution to this but I can't seem to figure out what I'm doing wrong. 我一直在搜索stackoverflow来找到解决方案,但似乎无法弄清楚我在做什么错。 I'm trying to select anything inside of "( )" brackets in primary/parent links within a menu and remove them with .replace() but nothing seems to be happening. 我正在尝试在菜单的主/父链接中选择“()”括号内的任何内容,然后使用.replace()将其删除,但似乎什么也没有发生。

JSFiddle: http://jsfiddle.net/1rndwz3u/1/ JSFiddle: http : //jsfiddle.net/1rndwz3u/1/

JS: JS:

jQuery(document).ready(function () { 
    jQuery(".menu-name-menu-category-menu ul.nav li > a").html().replace(/\(.*?\)/g,"");
});

HTML: HTML:

<div class="menu-name-menu-category-menu">
    <ul class="menu nav">
        <li class="first expanded dropdown active-trail">
            <a title="Heavy Equipment Rentals (0)" href="#">Heavy Equipment Rentals (0)<span class="caret"></span></a>
            <ul class="accordion-body">
                <li><a title="Excavator Rentals (2)" href="#">Excavator Rentals (2)</a></li>
                <li><a title="Mini Excavator Rentals (6)" href="#">Mini Excavator Rentals (6)</a></li>
                <li><a title="Skid Steer Rentals (9)" href="#">Skid Steer Rentals (9)</a></li>
            </ul>
        </li>
        <li class="active-trail"><a title="Tool Rentals (1)" href="#">Tool Rentals (1)</a></li>
        <li class="last expanded dropdown active-trail">
            <a title="Construction Rentals (0)" href="#">Construction Rentals (0) <span class="caret"></span></a>
            <ul class="accordion-body">
                <li><a title="Gator, UTV &amp; ATV Rentals (2)" href="#">Gator, UTV &amp; ATV Rentals (2)</a></li>
            </ul>
        </li>
    </ul>
</div>

I've tried using .html() and .text() but neither did anything, also tried grabbing just "Rentals" instead of a regex for the brackets and that didn't work either. 我已经尝试过使用.html().text()但是都没有做任何事情,也尝试仅使用“ Rentals”代替括号的正则表达式,这也没有用。 Help is greatly appreciated. 非常感谢您的帮助。 Thanks. 谢谢。

I haven't looked a the Fiddle yet but 我还没看过小提琴

jQuery(".menu-name-menu-category-menu ul.nav li > a").html().replace(/\(.*?\)/g,"");

returns a string and must be set back into the HTML. 返回一个字符串,必须将其设置回HTML中。

var HTML = jQuery(".menu-name-menu-category-menu ul.nav li > a").html().replace(/\(.*?\)/g,"");
jQuery(".menu-name-menu-category-menu ul.nav li > a").html( HTML );

You are editing the value but not doing anything with it. 您正在编辑值,但不对其执行任何操作。 Try wrapping it with one more html() call: 尝试用另一个html()调用包装它:

jQuery(document).ready(function () {
    var target =  jQuery(".menu-name-menu-category-menu ul.nav li > a");
    target.html(target.html().replace(/\(.*?\)/g,""));
});

working fiddle here 这里工作

jQuery(document).ready(function () { 
    $( ".menu-name-menu-category-menu ul.nav li.expanded > a" ).each(function( index,elem ) {
          $(elem).html($(elem).html().replace(/ *\([^)]*\) */g, ""));
    });

});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM