简体   繁体   English

如何在JavaScript中将HTML标签替换为另一个

[英]How can i replace HTML tag to another in javascript

I am new with js and playing with replace method. 我是js的新手,正在玩replace方法。 I have had no problems when replacing string for another string etc., but when im trying to do same with tags nothing happens.. 将字符串替换为另一个字符串等时,我没有任何问题,但是当我尝试使用标签执行相同操作时,什么也没发生。

Im trying to replace every 我正在尝试替换所有

  • tags for -tags. -tags的标签。 My function is below: 我的功能如下:

     function bonus() { var list = document.getElementsByTagName('li'); for (var i = 0; i < list.length; i++) { newList = document.getElementsByTagName('li')[i].innerHTML; newList = newList.replace('<li>', '<strong>'); newList = newList.replace('</li>', '</strong>'); document.getElementsByTagName('li')[i].innerHTML = (newList); //console.log(newList); } } 

  •  function bonus(){ var list=document.getElementsByTagName('li'); var len = list.length; for(var i=len-1; i>-1; i--){ var tmpItem = list[i] newList = tmpItem.outerHTML; newList = newList.replace('<li>','<strong>'); newList = newList.replace('</li>','</strong>'); tmpItem.outerHTML = newList; } } 

    I thought you might change your code like above, and there was still much space to optimize your code. 我以为您可以像上面那样更改代码,并且仍然有很多空间可以优化代码。 go ahead <( ̄︶ ̄)> 继续<( ̄︶ ̄)>

    First of all you should never replace a list-item with another element like that. 首先,您永远不要将列表项替换为其他元素。 A list item must always be a child of an ul or ol elelment, and ul and ol elements should not have any other immediate child that isn't a li. 列表项必须始终是ul或ol元素的子元素,并且ul和ol元素不应有其他不是li的直接子元素。

    However, this doesn't work because the li is an html element and not a text string and the inner HTML of an html elemnt doesn't contain the tag itself. 但是,这是行不通的,因为li是一个html元素,而不是文本字符串,并且html elemnt的内部HTML不包含标签本身。 It may contain children and those children's tags are part of the innerHTML, everything inside the element/tag itself is the innerHTML. 它可能包含子项,并且那些子项的标签是innerHTML的一部分,元素/标签本身内部的所有内容都是innerHTML。

    An example to clarify: 一个例子来澄清:

    <ul>
        <li>one<strong>second one</strong></li>
        <li>two<strong>second two</strong></li>
        <li>three<strong>second three</strong></li>
        <li>four</li>
    </ul>
    

    Looping through all list items accessing elements as you've describe 正如您所描述的那样遍历所有列表项访问元素

    for(var i=0; i<list.length;i++) {
        console.log("==>> " + document.getElementsByTagName("li")[i].innerHTML);
    }
    

    Will output the following to the console: 将输出以下内容到控制台:

     ==>> one<strong>second one</strong>
    
     ==>> two<strong>second two</strong>
    
     ==>> three<strong>second three</strong>
    
     ==>> four
    

    If you want to make all li elements strong they should be nested as this: 如果要增强所有li元素的强度,则应将它们嵌套如下:

    <li><strong>Some text</strong></li>
    

    To Achieve this one way to do it would be: 实现此一种方法的方法是:

    var list = document.getElementsByTagName("li");
    
    for(var i=0; i<list.length; i++) {
        var listItem = list[i];
        listItem.innerHTML = "<strong>" + listItem.innerHTML + "</strong>";
    }
    

    If you want to convert all li elements to strong elements you must first remove them from the list... 如果要将所有li元素转换为强元素,则必须首先将其从列表中删除...

    Thanks alot for all of you. 非常感谢大家。 I knew that i can't change li for strong in real world, but i just tried to figure out if its possible to do so with simple loop. 我知道我无法在现实世界中变强,但我只是想找出是否有可能通过简单的循环来实现。

    My example html is full of lists, so thats why i used li instead of h2 to h3 or something like that. 我的示例html充满了列表,所以这就是为什么我使用li而不是h2到h3之类的原因。 Outer html was new thing for me, and solution for this one. 外部html对我来说是新事物,而对于这一解决方案而言。 However Kim Annikas answer helped me with other question about modifying lists: I did this: 但是Kim Annikas的回答帮助我解决了有关修改列表的其他问题:我做到了:

    function replace(){
    
    var list=document.getElementsByTagName('li');
    for(var i=0;i<list.length;i++){
    newText="<strong>Replaced!</strong>";
    var listItem=list[i];
    listItem.style.color="red";
    listItem.innerHTML=newText;
    

    } } }}

    ..and now it seems that i have learnt how to modify tags as well as text inside of it ;) ..现在看来,我已经学会了如何修改标签以及标签内部的文本;)

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

    相关问题 如何使用 JavaScript 在 HTML 中用另一个脚本标签替换脚本标签? - How to replace a script tag with another in HTML with JavaScript? Javascript:如何替换字符串的内容,而不替换HTML标记中的内容? - Javascript: How can I replace the contents of a string but not in an HTML tag? 如何使用javascript中的正则表达式查找和替换特定HTML标签中的样式? - How can i find and replace style in specific HTML tag with regex in javascript? 使用javascript如何识别HTML标签? - With javascript how can I identify the HTML tag? 如何插入标签<br>使用 JavaScript 插入另一个标签? - How can I insert a tag <br> into another tag using JavaScript? 如何使用“.replace()”和“.html()”将纯文本转换为图像标记? - How can I use “.replace()” and “.html()” to turn plaintext into an image tag? 如何替换HTML标记中的文本,而不替换href链接中的文本? - How can I replace the text in an HTML tag but not in an href link? 如何用html标签替换字符? (丰富的文字) - How can I replace characters with html tag? (enriching text) 如何替换/更新字符串中的特定 HTML 标记 - How can I replace/update specific HTML tag inside string 如何对 HTML标签进行条带化并将其替换为<h> ?</h> - How can I stripe the HTML <b> tag and replace it with <h>?
     
    粤ICP备18138465号  © 2020-2024 STACKOOM.COM