简体   繁体   English

如何通过onClick()javascript查看特定html标记的源代码而不显示其CSS?

[英]How to see the source code of a particular html tag by onClick() javascript without showing its css?

Hi everyone, here am trying to display the source code of a particular div by onclick 大家好,这里我试图通过onclick显示特定div的源代码

javascript function. javascript函数。 But the result am getting is , when i click on the div, am seeing the 但结果是,当我点击div时,我看到了

whole source code though am trying to make only the particular source code of a div to get 整个源代码虽然我试图只使得div的特定源代码得到

displayed. 显示。 anyone please highlight me what am doing wrong here..! 有人请突出我在这里做错了什么..! and what to do to make 以及如何做

it work in the way its expected. 它以预期的方式工作。

  <html>
    <head>
        <title></title>
    <script type="text/javascript">
    function viewsource(){
       var oldHTML = document.getElementById('para').innerHTML;
       var newHTML = "" + oldHTML + "";
       var newHTML = newHTML.replace(/</g,"&lt;");
       var newHTML = newHTML.replace(/>/g,"&gt;");
       var newHTML = newHTML.replace(/\t/g,"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;");
        document.getElementById('para').innerHTML = newHTML;
    }
    </script>

    </head>

    <body>
    <p id='para'><strong>This is my <span style="border: 1px solid #ccc;">test text</span> to check if it works</strong></p> 
    <input type='button' onclick='viewsource()' value='View Source'/>
    </body>
    </html>

Additional notes: 补充笔记:

In the above code, when the button is clicked, the paragraph tag with the id of para will display...but it shows its css too. 在上面的代码中,当单击按钮时,id为para的段落标记将显示...但它也显示了它的css。 I want to display only the html tag without the css style attribute. 我想只显示没有css样式属性的html标签。

I don't want the content using innerHTML but i want the whole div including with the div id.(eg.) <div id='softros'><img src='/images/bgrade.jpg' /></div> 我不希望内容使用innerHTML,但我希望整个div包括div id。(例如) <div id='softros'><img src='/images/bgrade.jpg' /></div>

您是否考虑过在java脚本处理程序中使用:

document.getElementById(YOUR_DIV_ID_GOES_HERE).innerHTML

You can get the HTML of the div using .innerHTML or .outerHTML but you need the encode it before you can display it. 您可以使用.innerHTML或.outerHTML获取div的HTML,但在显示之前需要对其进行编码。 Otherwise the html gets renderd by the browser. 否则,html会被浏览器渲染。 In PHP you could use htmlencode javascript has no function for this but you could use this htmlencode . 在PHP中你可以使用htmlencode javascript没有这个功能,但你可以使用这个htmlencode

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

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