[英]How to match copy with parentheses Javascript
I'm trying to find a specific Product Name on a page, and if this matches then I want to add " - 10% Discount" at the end of this name.我正在尝试在页面上查找特定的产品名称,如果匹配,那么我想在此名称的末尾添加“- 10% 折扣”。
The issue I have is at the end of the Product Name I have in parentheses "(1+1)" how can I include this in the variable findCopy
so my outcome will be: "Product Name (1+1) - 10% Discount"我遇到的问题是在括号“(1+1)”中的产品名称末尾我如何将其包含在变量
findCopy
因此我的结果将是:“产品名称 (1+1) - 10% 折扣”
var findCopy = 'Product Name'; // The below is what I'm trying to achieve to get // var findCopy = 'Product Name (1+1)'; var targetThis = document.querySelector('.copy'); targetThis.innerText = targetThis.textContent.replace(new RegExp(findCopy), findCopy + ' - 10% Discount');
<p class="copy">Product Name (1+1)</p>
You know the product name like Product Name (1+1)
, so you need to find the tag has the same value and then append it - 10% Discount
您知道产品名称,例如
Product Name (1+1)
,因此您需要找到具有相同值的标签,然后将其附加- 10% Discount
var findCopy = 'Product Name (1+1)'; // The below is what I'm trying to achieve to get // var findCopy = 'Product Name (1+1)'; var targetThis = [...document.querySelectorAll('.copy')].filter(tag => tag.textContent.includes(findCopy))[0] targetThis.innerText = targetThis.textContent + ' - 10% Discount';
<p class="copy">Product Name (1+1)</p>
Update without spread operator :没有传播运算符的更新:
var findCopy = 'Product Name (1+1)'; // The below is what I'm trying to achieve to get // var findCopy = 'Product Name (1+1)'; var array = document.querySelectorAll('.copy'); var targetThis; for (var i = 0; i < array.length; i++) { if (array[i].textContent.includes(findCopy)) targetThis = array[i]; } targetThis.innerText = targetThis.textContent + ' - 10% Discount';
<p class="copy">Product Name (1+1)</p>
Heavily based on Alireza Ahmadi solution, but allowing you to change several products at once:很大程度上基于 Alireza Ahmadi 解决方案,但允许您一次更改多个产品:
var findCopy = 'Product Name'; // The below is what I'm trying to achieve to get // var findCopy = 'Product Name (1+1)'; var targetThis = [...document.querySelectorAll('.copy')].filter(tag => tag.textContent.includes(findCopy)) targetThis.forEach(el => el.innerText = el.textContent + ' - 10% Discount');
<ol> <li class="copy">Product Name (1+1)</li> <li class="copy">Product (1+1)</li> <li class="copy">Product Name</li> <li class="copy">(1+1) Product Name</li> <li class="copy">Potato</li> </ol>
I don't know whether you can use jQuery or not.我不知道你是否可以使用jQuery。 Here I did a solution using jQuery.
在这里,我使用 jQuery 做了一个解决方案。 Of course it works for multiple
当然它适用于多个
and change them all并将它们全部改变
HTML: HTML:
<div class="new">
<p>
product Name 1
</p>
<p>
product Name 2
</p>
<p>
product Name 3
</p>
<p>
product Name 1 abcde
</p>
</div>
<button onclick="test('product Name 1')">test</button>
JS code: JS代码:
function test(string) {
$("p:contains('" + string + "')").each(function () {
var sHtml = $(this).html();
$(this).html(sHtml + " - 10% Discount");
})
}
Finally it will change the content like below:最后它会改变如下内容:
product Name 1 - 10% Discount产品名称 1 - 10% 折扣
product Name 2产品名称 2
product Name 3产品名称 3
product Name 1 abcde - 10% Discount产品名称 1 abcde - 10% 折扣
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.