简体   繁体   English

如何在课后定位标签?

[英]How to target tag after a class?

<div class="pre-class">
    <div>100%</div>
</div>

I'm trying to remove the percent (%) on the text inside the div after the class pre-class with textContent.replace("%","") but I just can't target that specific div我正在尝试在使用textContent.replace("%","")进行类pre-class之后删除 div 内文本的百分比 (%) 但我只是无法定位那个特定的 div

(I'm working on a longer website with a lot of divs and I can't add an ID to it because it's from a shortcode.) (我正在一个有很多 div 的更长的网站上工作,我无法为其添加 ID,因为它来自短代码。)

I thought I could do something like this:我以为我可以做这样的事情:

var textContent = document.getElementsByClassName('gamipress-progress-bar-completed').getElementsByTagName('div'); 

You're basically there.你基本上在那里。 Don't forget that getElementsByClassname returns an array, so simply use [0] to retrieve the element.不要忘记 getElementsByClassname 返回一个数组,所以只需使用[0]来检索元素。 You'll see it working in the snippet below:您将在下面的代码段中看到它的工作原理:

 var textContent = document.getElementsByClassName('pre-class')[0].getElementsByTagName('div')[0].innerHTML; console.log(textContent)
 <div class="pre-class"> <div>100%</div> </div>

 let containers = document.getElementsByClassName("pre-class"); for (var i = 0; i<containers.length; i++) { let current = containers[i]; let textNode = current.children[0]; textNode.innerText = textNode.innerText.replace(/(\d)%/, '$1'); };
 <div class="pre-class"> <div>100%</div> </div>

Alternatively, you could use element.querySelector() (or querySelectorAll ) to find the correct element(s) to replace.或者,您可以使用element.querySelector() (或querySelectorAll )找到要替换的正确元素。

 let textNode = document.querySelector(".pre-class > div"); // use querySelectorAll in case there can be multiple matches. textNode.innerText = textNode.innerText.replace(/(\d)%/, '$1');
 <div class="pre-class"> <div>100%</div> </div>

You can use querySelector您可以使用querySelector选择器

 let div = document.querySelector('div.pre-class > div'); div.innerText = div.innerText.replace('%', '')
 <div class="pre-class"> <div>100%</div> </div>

If the div will be the first direct child of the pre-class div, and you have one element with "pre-class" class, this will work如果 div 将是 pre-class div 的第一个直接子元素,并且您有一个带有“pre-class”类的元素,这将起作用

 const textContent = document.querySelector('.pre-class').firstElementChild.textContent; console.log(textContent.replace('%', ''))
 <div class="pre-class"> <div>100%</div> </div>

Use the child selector (>).使用子选择器 (>)。

let textDiv=document.querySelector(".pre-class > div");
textDiv.textContent=textDiv.textContent.replace("%","");

This will replace the first direct div inside .pre-class.这将替换 .pre-class 中的第一个直接 div。 You can adjust the position of divs using pseudo classes.您可以使用伪类调整 div 的位置。 Like for example, if you want to select the second div inside .pre-class, you would use:例如,如果你想选择 .pre-class 中的第二个 div,你可以使用:

<div class="pre-class">
    <div>100%</div>
     <div>200%</div>
    <div>300%</div>
</div>

let textDiv=document.querySelector(".pre-class > div:nth-child(2)");
textDiv.textContent=textDiv.textContent.replace("%","");

 const content = document.querySelector('.pre-class > div').innerHTML; content.replace("%", "");
 <div class="pre-class"> <div>100%</div> </div>

This is another way of selecting the div nested in your .pre-class div.这是另一种选择嵌套在 .pre-class div 中的 div 的方法。 Perhaps not the best way of doing this but it's handy to know querySelector works.也许这不是最好的方法,但知道 querySelector 的工作原理很方便。

If you have lots of divs inside div.pre-class , its better to add specific data attribute to each child div and select the desired div using this:如果您在div.pre-class中有很多divs ,最好将特定的数据属性添加到每个子div并使用以下方法选择所需的div

 < div class = 'pre-class' > <div data-order = 'first' > 1 < /div> <div data-order = 'second' > 2 < /div> <div data-order = 'third' > 3 < /div> </div> /////////// document.querySelector('div[data-order="first"]')

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

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