• js的replace, 高亮, insertAdjacentHTML , tbody.innerHTML


    var str ="123";console.log(str.replace(/\,/g, "")); //输出 123
    var str ="123";console.log(str);//输出123
    var str ="123";str.replace(/\,/g, ""); //输出  "123"

    var str ="123,123";str.replace(/\,/g, "");//输出 123123
    //var reg2 = new RegExp("\{0}", "g")
    //new RegExp("\{" + (i - 1) + "}", "g")

             var str = "123,  1  2  3";
                str =   str.replace(/ /g, "");//输出 123,123

    把字符串中的引号 替换为指定字符 X
               var  str0 =str.replace(/"/g, 'X'); 
    把字符中的引号 替换为反斜杠引号 
        var  str0 =str.replace(/"/g, '\"'); 
                var t1 = document.getElementById("js1");
                var htmltxt = t1.textContent;
                var html = t1.innerHTML;
                t1.textContent = '<span style="color: red;">123<span>';
                //t1.innerHTML = "123";
                t1.innerHTML = '<span style="color: red;">123<span>';
    <body>
        <pre>
            <code id="js1">
                var clacounts = function (string, ele) {
                var count = 0; //定义
                pos = str.indexOf(ele); ///测试
                while (pos != -1) { //ok
                count++; //搜索
                pos = str.indexOf(ele, pos + 1); //查询
                }
                return count;
                }
            </code>
        </pre>
        <input type="button"  value="test" onclick="test()"/>
    
        <script>
            var clacounts = function (string, ele) {
                var count = 0; //定义
                pos = str.indexOf(ele); ///测试
                while (pos != -1) { //ok
                    count++; //搜索
                    pos = str.indexOf(ele, pos + 1); //查询
                }
                return count;
            }
        </script>
        <script>
            function test() {
                var t1 = document.getElementById("js1");
    
                var html = t1.innerHTML;
    
                t1.innerHTML = '<span style="color: red;">123</span>';
    
                var t2 = html.split("
    ");
                
                for (var i = 0; i < t2.length; i++) {
                    t2[i] = t2[i].replace('var', '<span style="color: red;">var</span>');
                }
                var test = t2.join('
    ');
                t1.innerHTML = test;
            }
        </script>
    </body>
    element.insertAdjacentHTML(position, text);
    
    position 是相对于 element 元素的位置,并且只能是以下的字符串之一:
    
    beforebegin:在 element 元素的前面。
    
    afterbegin:在 element 元素的第一个子节点前面。
    
    beforeend:在 element 元素的最后一个子节点后面。
    
    afterend:在 element 元素的后面。
    
    text 是字符串,会被解析成 HTML 或 XML,并插入到 DOM 树中。
    
    // <div id="one">one</div> 
    var d1 = document.getElementById('one'); 
    d1.insertAdjacentHTML('afterend', '<div id="two">two</div>');
    
    // 此时,新结构变成:
    // <div id="one">one</div><div id="two">two</div>
                   //
                        //type.firstElementChild   //第一个子元素节点
                        //type.childElementCount   //子元素总数
                        //type.lastElementChild   //最后一个子元素节点
                        //type.parentElement  //父元素
                        //type.parentNode   //父节点
                        //
  • 相关阅读:
    Codeforces Round #649 (Div. 2) D. Ehab's Last Corollary
    Educational Codeforces Round 89 (Rated for Div. 2) E. Two Arrays
    Educational Codeforces Round 89 (Rated for Div. 2) D. Two Divisors
    Codeforces Round #647 (Div. 2) E. Johnny and Grandmaster
    Codeforces Round #647 (Div. 2) F. Johnny and Megan's Necklace
    Codeforces Round #648 (Div. 2) G. Secure Password
    Codeforces Round #646 (Div. 2) F. Rotating Substrings
    C++STL常见用法
    各类学习慕课(不定期更新
    高阶等差数列
  • 原文地址:https://www.cnblogs.com/enych/p/9828216.html
Copyright © 2020-2023  润新知