Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-core/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用javascript替换HTML值_Javascript - Fatal编程技术网

使用javascript替换HTML值

使用javascript替换HTML值,javascript,Javascript,我有以下HTML代码,并希望使用javascript替换它们 原始代码 <td class="tabButton dijitHidden tweetsTabButton selectedTab" dojoattachpoint="tweetsTabButton" data-type="twitter" style="display: none;"> Tweets <div class="arrow-up"><div class="arrow-up-content"&

我有以下HTML代码,并希望使用javascript替换它们

原始代码

<td class="tabButton dijitHidden tweetsTabButton selectedTab" dojoattachpoint="tweetsTabButton" data-type="twitter" style="display: none;">
Tweets
<div class="arrow-up"><div class="arrow-up-content"></div></div>
</td>

推特
换成

<td  dojoattachpoint="tweetsTabButton" data-type="twitter" style="display: none;">
<h2>Tweets</h2>

</td>

推特

根据问题内容和当前的评论,您需要:

  • 将文本
    Tweets
    包装为元素,特别是
  • td
    中删除
    class
    属性以禁用CSS
  • 使用纯JavaScript
  • 假设存在
    ID
    并使用
    数据类型
    属性作为验证 看看这个例子

    javaScript

    // get the table by ID
    var table = document.getElementById("table-id");   
    
    // get the table cells
    var cells = table.getElementsByTagName("td");   
    
    // for each table cell found
    for (var i = 0; i < cells.length; i++) {   
    
        // get the attribute 'data-type'
        var status = cells[i].getAttribute("data-type");
    
        // if 'data-type' contains the value 'twitter'
        if ( status == "twitter" ) {   
    
            // create the 'h2' element
            var h2 = document.createElement("h2");
    
                // append a textnode to the created element with the text 'Tweets'
                h2.appendChild(document.createTextNode('Tweets'));
    
            // remove the current text 'Tweets'
            cells[i].removeChild(cells[i].firstChild);
    
    
            // remove the class atrribute
            cells[i].removeAttribute('class');
    
            // append the new element to the current 'td'
            cells[i].appendChild(h2);
        }  
    }
    
    //按ID获取表
    var table=document.getElementById(“表id”);
    //获取表格单元格
    var cells=table.getElementsByTagName(“td”);
    //对于找到的每个表格单元格
    对于(var i=0;i

    注意:可以优化,但您可以看到发生了什么。此外,这些评论还可以指导您在生产过程中删除这些内容。

    还有另一种方法:

    //index [0] assumes you want to modify the first td matching this set of class rules..
    var td = document.getElementsByClassName("tabButton dijitHidden tweetsTabButton selectedTab")[0];
    
    //remove the attributes, as requested
    td.removeAttribute('class');
    td.removeAttribute('dojoattachpoint');
    
    //sets the innerHTML of the element, overwriting the original one
    td.innerHTML = '<h2>Tweets</h2>';
    
    使其动态可见。不要忘记在DOM就绪/加载后运行脚本


    所以。。。删除
    ,并将
    推文
    替换为
    推文
    ,然后删除确切的文本
    ?如果不是这样,那么这些数据有什么共同点?@Marcin我喜欢这个链接。这将有助于我在这个网站上花费更少的时间。@Hippo我将它添加到书签中,这样我就可以将它拖到评论框中。@bumblebee:为什么这个问题用css标记?你试过什么?您是否使用任何JavaScript框架。如果您需要帮助,我们需要更多信息。精确的问题-->精确的答案@Marcin现在这是有用的信息!是时候把所有Meta的“堆栈溢出不是帖子”都标记为书签了。顺便说一句,欢迎发表评论。谢谢你的帖子@Fabricio。我也会试试这个
    td.style.display = "table-cell";