Javascript保护要删除的文本行

javascript to protect text line to be removed?

本文关键字:文本 删除 保护 Javascript      更新时间:2023-09-26

我想做一个函数,可以做以下事情

  1. 首先,它将查看html/text中的行是否存在。

  2. 那么它将删除<style></style>标签中的所有属性

如果是,如何处理?我不擅长javascript。

这是我尝试过的

<html>
<head>
<title>My page</title>
<style>
body{
color:#fff;
background:#000;
}
</style>
<script>
    function myFunction()
    {
    var str="My copyright text"
    var n=str.search("My copyright text");
    //n = 0 or -1
    }
    </script>
</head>
<body>
<div id="footer">my Copyright text</div>
</body>

那么这段代码将会给我0或者-1如果它在var str="中找到我的文本它将返回0如果它将被改变它将返回-1

我想做的是首先我想让var str="从一个div id所以它会从div id的文本并将其放入var str然后代码将看到0或。1的值如果它是-1它会删除所有的<style></style>标签

我正在编辑这个问题,因为我有一个新的问题

我有以下代码
    <!DOCTYPE html>
<html>
<style>
 #footer{
  border:1px solid;
background-color:#999;
color:#fff;
padding:10px;
}
</style>
<body>
<p id="footer">Copyright google.com and yahoo.com</p>
<button onclick="myFunction()">Test</button>
<script>
function myFunction()
{
    var str=document.getElementById('footer').innerHTML;
    var n=str.search("Copyright google.com and yahoo.com");
    if(n == -1)
    {
        var elem = document.getElementsByTagName('style')[0]; // this will get the first which should be the style tag in the head element, if you have others you may need to modify
        elem.parentNode.removeChild(elem);
    }
}
</script>
</body>
</html>

这个代码保护我的文本不被删除或更改但我想把我的链接放在文本中如果把我的链接也就是HTML源我的代码不识别我的链接任何人都可以删除链接有想法吗?

如果给定的字符串与div的内容匹配,下面是如何删除样式标签的方法:

function myFunction()
{
    var str=document.getElementById( <div id> ).innerHTML;
    var n=str.search("My copyright text");
    if(n == 0)
    {
        var elem = document.getElementsByTagName('style')[0]; // this will get the first which should be the style tag in the head element, if you have others you may need to modify
        elem.parentNode.removeChild(elem);
    }
}