简体   繁体   English

jQuery替换字符串中字符的方法

[英]jQuery way to Replace character from a string

I have a generated navigation, The first n items have _ in front of them to always be at the top of list. 我有一个生成的导航,前n个项目前面有_ ,始终位于列表的顶部。 What I want to accomplish now is to remove that _ from the front-end of the site. 我现在想要完成的是从站点的前端删除该_

I tried to do this with the jQuery code below which almost worked but it is also changing the text of other nav items. 我尝试使用下面的jQuery代码执行此操作,但该代码几乎起作用了,但是它也在更改其他导航项的文本。

See full mark-up on JS Fiddle: https://jsfiddle.net/1fz661o4/ 查看JS Fiddle的完整标记: https//jsfiddle.net/1fz661o4/

Code: 码:

$(function() {
  $('.dropdown-submenu').html($('.dropdown-submenu').html().replace(/\_/g,''));
});

You could just change it to loop over the anchors. 您可以更改它以遍历锚点。

$(function() {
    $('.dropdown-submenu > a').each(function() {
        $(this).text($(this).text().replace(/\_/g, ''));
    });
});

https://jsfiddle.net/utk12jdt/1/ https://jsfiddle.net/utk12jdt/1/

So basically you want to remove only the underscores in anchors that are direct children of .dropdown-submenu ? 因此,基本上,您只想删除锚点中属于.dropdown-submenu的下划线? In your fiddle, replace $('.dropdown-submenu') with $('.dropdown-submenu > a') . 在您的提琴中,将$('.dropdown-submenu')替换$('.dropdown-submenu > a')

JSFiddle: https://jsfiddle.net/26qLofz8/ JSFiddle: https ://jsfiddle.net/26qLofz8/

$(function() {
      $('.dropdown-submenu > a').html($('.dropdown-submenu > a').html().replace(/\_/g,''));
});

Edit: 编辑:

after your response, the question now is clear. 在您做出回应后,现在的问题很清楚了。 To prevent your <li> 's from all becoming Software one, you will need to loop over all the elements you want to replace, like so: 为了防止您的<li>全部成为Software One,您需要遍历要替换的所有元素,如下所示:

$(function() {
  $('.dropdown-submenu > a').each(function(i, el) {
      $(el).html($(el).html().replace(/\_/g,''));
  });
});

JSFiddle: https://jsfiddle.net/n7ms67k0/ JSFiddle: https ://jsfiddle.net/n7ms67k0/

You can use the .html() overload with a function that provides the existing html: 您可以将.html()重载与提供现有html的函数一起使用:

$('.dropdown-submenu').html(function(index, html) { return html.replace(/\_/g,''); });

Updated fiddle: https://jsfiddle.net/1fz661o4/2/ 更新的小提琴: https : //jsfiddle.net/1fz661o4/2/


Alternatively, you can loop over each record: 或者,您可以遍历每条记录:

$('.dropdown-submenu').each(function() {
   $(this).html($(this).html().replace(/\_/g,''));
});

Simply replacing the HTML will remove any event listeners and might also cause issues in the future if you have links with underscores in the URL etc. 只需替换HTML即可删除所有事件侦听器,并且如果您在URL等中使用带下划线的链接,将来还会引起问题。

What you should do, is go through the elements and change the value of the text nodes. 您应该做的是遍历元素并更改文本节点的值。 I'm not sure how to do it reliably in jQuery, but here is how you would do it with plain JavaScript DOM. 我不确定如何在jQuery中可靠地实现它,但是这是使用纯JavaScript DOM的方法。

function replaceUnderscore(index, el) {
  switch (el.nodeType) {
    case 3: // text node
    el.nodeValue = el.nodeValue.replace(/_/g, '');
    break;

    case 1: // normal element
    for (var i = 0, child; child = el.childNodes[i]; i++) {
      replaceUnderscore(i, child);
    }
    break;
  }
}

To apply this to elements you found in jQuery, you just do 要将其应用于您在jQuery中找到的元素,只需执行

$('.class').each(replaceUnderscore);

Not tested but I think this should work. 未经测试,但我认为这应该工作。

That being said, do you have to do this in JavaScript? 话虽这么说,您必须使用JavaScript进行此操作吗? Ideally this kind should be performed on the server side instead. 理想情况下,应在服务器端执行这种操作。 If it's a CMS you might be able to write a little plugin for this purpose. 如果是CMS,则可以为此目的编写一个小插件。

You need to make your selector more specific. 您需要使选择器更具体。 Either give it a unique class or an ID and select based on that. 给它一个唯一的类或一个ID,然后根据它进行选择。

<ul class="dropdown-submenu my-custom-menu">

And the jQuery would change to: jQuery将变为:

$(function() {
  var menu = $('.my-custom-menu'); // save the query since we use it twice
  menu.html(menu.html().replace(/\_/g,''));
});

NOTE: You're using id s incorrectly since they're meant to be globally unique. 注意:您错误地使用id因为它们是全局唯一的。 You shouldn't have id="row" multiple times. 您不应多次使用id="row" That's what classes are for. 那就是上课的目的。

You must go through each element with the .dropdown-submenu class using the jQuery each function so you will only affect one element at a time. 您必须使用jQuery each函数使用.dropdown-submenu类遍历每个元素,这样您一次只会影响一个元素。 Your JavaScript code would look like this. 您的JavaScript代码如下所示。

$(".dropdown-submenu").each(function() {
    //this is the current .dropdown-submenu element
    $(this).html($(this).html().replace(/\_/g,''));
});

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

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