简体   繁体   English

在DOM中查找元素并替换

[英]Find element and replace in DOM

i am in a bit of a jam here, i am trying to find all div elements that are inside of another div container and then replace the matches with a new div using just javascript. 我在这里有点堵塞,我试图找到所有div元素在另一个div容器内,然后使用javascript替换匹配新div。

Here is a description: 这是一个描述:

I have these divs 我有这些div

<div id="container">
    <div class="oldclass">latency</div>
    <div>latency</div>
    <div>latency</div>
    <div class="oldclass">latency</div>
    <div>latency</div>
    <div>latency</div>
    <div class="oldclass">latency</div>
    <div>latency</div>
    <div>latency</div>
    <div class="oldclass">latency</div>
    <div>latency</div>
    <div>latency</div>
    <div class="oldclass">latency</div>
</div>

And what i want to achieve is this 而我想要实现的是这一点

<div id="container">
    <div class="newclass1">latency</div>
    <div>latency</div>
    <div>latency</div>
    <div class="newclass2">latency</div>
    <div>latency</div>
    <div>latency</div>
    <div class="newclass1">latency</div>
    <div>latency</div>
    <div>latency</div>
    <div class="newclass2">latency</div>
    <div>latency</div>
    <div>latency</div>
    <div class="newclass1">latency</div>
</div>

But i am having a really hard time with this, i already used childNodes, .match(), replace(), etc etc 但我真的很难用这个,我已经使用了childNodes,.match(),replace()等等

Any help is very much appreciated and thank you for your time 非常感谢任何帮助,感谢您的时间

In the following code, i select all div nodes from the document and loop through them. 在下面的代码中,我从文档中选择所有div节点并循环遍历它们。 If the classname of the node equals "oldclass", rename the classname to "newclass". 如果节点的类名等于“oldclass”,则将类名重命名为“newclass”。

elements = document.getElementsByTagName("div");
firstChanged = false;
for(i=0; i < elements.length; i++)
{
  if(elements[i].className == "oldclass")
  {
    elements[i].className = (firstChanged) ? "newclass2" : "newclass1";
    firstChanged = !firstChanged;
  }
}

Edit: The firstChanged boolean checks whether the script has come to the point of changing the first matching div. 编辑: firstChanged布尔值检查脚本是否已经到了更改第一个匹配div的位置。

Why not try using jQuery? 为什么不尝试使用jQuery? With jQuery you can easily do it as follows: 使用jQuery,您可以轻松地执行以下操作:

$("div.oldclass:even").addClass("newclass1");
$("div.oldclass:odd").addClass("newclass2");

This will add "newclass1" to all even indexed DIVs with "oldclass" as CSS class, and "newclass2" to all odd indexed DIVs with "oldclass" as CSS class. 这将为所有偶数索引的DIV添加“newclass1”,其中“oldclass”为CSS类,将“newclass2”添加到所有奇数索引的DIV,其中“oldclass”为CSS类。

First, do 首先,做

nodes = document.getElementsByClassName('oldclass');

or 要么

nodes = document.querySelectorAll('.oldclass');

That is a NodeList which can be accessed similarly to an array, then, do what you need to do. 这是一个NodeList,可以类似于数组访问,然后,做你需要做的事情。

I'm not sure if you're asking to change the CSS class name, or if you're asking to replace the div completely, and if so, where from. 我不确定你是否要求更改CSS类名,或者你是否要求完全替换div,如果是,那么从哪里来。

Copy the NodeList to a static array first: 首先将NodeList复制到静态数组:

nodes = Array.prototype.slice.call(nodes);

We don't want nodes to be a NodeList as these are live and can have unpredictable results when we're modifying them. 我们不希望nodes成为NodeList因为它们是实时的,并且在我们修改它们时会产生不可预测的结果。

If you want to replace the selected elements with new ones, 如果要用新的元素替换所选元素,

nodes[i].parentNode.replaceChild(newChild, nodes[i]);

If you want to change the class names, like in your example, do the first two lines then this: 如果你想改变类名,就像在你的例子中那样,先做两行,然后这样做:

nodes.forEach(function(node, i) {
    node.className = 'newclass' + (i % 2 ? 1 : 2);
});

I'm not quite clear on your question (the intent of your question and your example differ) so I've made suggestions for both possible intents. 我对你的问题不太清楚(问题的意图和你的例子有所不同)所以我已经为两种可能的意图提出了建议。

I think this should do 我认为应该这样做
Javascript Only 仅限Javascript

//Select the container  
var div = document.getElementById("container");  
//find all div child with class = oldclass  
var divs = div.getElementsByClassName("oldclass");  
//then replace it  
for(var i = 0; i < divs.length; i++)  
{  
    if(i % 2)  
        divs[i].className = "newclass1";  
    else  
        divs[i].className = "newclass2";  
}
`  
//Using Jquery:  
`$("#container div.oldclass").each(function(i){  
        $(this).removeClass("oldclass").addClass("newclass" + (i % 2 ? 1 : 2));  
});

Using Jquery 使用Jquery

var arr = $("#container").children(".oldclass");
var classname = "newclass1";
for(var i=oi<arr.length;i++) {
 $(arr[i]).attr("class",classname);
}

我做了这个使用jquery的小提琴: http//jsfiddle.net/Vk687/

If you using jQuery, you can use .each() function like this: 如果你使用jQuery,你可以像这样使用.each()函数:

jQuery(".oldclass").each(function(index, domEle) {
    domEle.className = "newclass" + (index%2+1);
});

Result: http://jsfiddle.net/Achilleterzo/79kfF/ 结果: http//jsfiddle.net/Achilleterzo/79kfF/

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

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