简体   繁体   English

将元素/类添加到 javascript/jquery 字符串中的第一个唯一单词

[英]add element/class to a first unique word from a string in javascript / jquery

I want to insert a tag from the first unique word from a string using javascript / jquery.我想使用 javascript/jquery 从字符串中的第一个唯一单词插入一个标签。 It is something like:它是这样的:

 jQuery(document).ready(function($){ $(".product_title:contains(NEW)").replace('NEW', '<span class="new">NEW</span>'); });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> FROM: <h2 class="product_title">NEW Jeep Wrangler</h2> INTO: <h2 class="product_title"><span class="new">NEW</span>Jeep Wrangler</h2> FROM: <h2 class="product_title">REBUILT Jeep Wrangler</h2> INTO: <h2 class="product_title"><span class="rebuilt">REBUILT</span>Jeep Wrangler</h2>

I tried below approach to replace the first word with html code but didn't work: I received a Uncaught TypeError: $(...).replace is not a function on console.我尝试了以下方法用 html 代码替换第一个单词,但没有用:我收到了一个Uncaught TypeError: $(...).replace is not a function on console。

How to achieve this?如何实现这一目标? I can't find any examples on the web that is applying this similar approach.我在网上找不到任何应用这种类似方法的例子。 Please advise.请指教。

UPDATE: I think I'm close, I use below approach:更新:我想我很接近,我使用以下方法:

$('.product_title').each(function() {
    var text = $(this).text();
    $(this).text(text.replace('NEW', '<span class="cust-woo-title-tag-new">NEW</span>')); 
});

But the tags are displaying on the front end.但是标签显示在前端。

Something like this could work:这样的事情可以工作:

$('h2').each((i)=>{
 txt=$(i).text().split(' ')
i_html = `
<span class ='${txt[0]}.toLocaleLowerCase()' >${txt.pop(0)}</span> ${txt.join(' ')}
`
$(i).html(i_html)
})

If you just want to do it for certain words, create a array with them and check with keywords.include(txt[0])如果您只想对某些单词执行此操作,请使用它们创建一个数组并检查keywords.include(txt[0])

  1. get all the product_title that contains "NEW"获取所有包含“NEW”的 product_title
  2. store the previous html value (eg. NEW Jeep Wrangler )存储之前的 html 值(例如NEW Jeep Wrangler
  3. remove the "NEW" word ( NEW Jeep Wrangler becomes Jeep Wrangler)删除“新”字样( NEW Jeep Wrangler变成吉普牧马人)
  4. update the innerHTML更新innerHTML
     $(document).ready(function($){ //get all the product_title that contains "NEW" let contains_new = $(".product_title:contains(NEW)"); for(let x = 0; x < contains_new.length; x++) { //store the previous html value let prev_html = contains_new[x].innerHTML; //remove the "NEW" word in the prev_html let new_html = prev_html.replace('NEW',''); //update the inner html contains_new[x].innerHTML = `<span class"new">NEW</span>${new_html}`; } });
     <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <h2 class="product_title">NEW Jeep Wrangler</h2> <h2 class="product_title">NEW Jeep Wrangler</h2>

try to inspect element the result to view the html structure.尝试检查元素结果以查看html结构。

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

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