开发者

Javascript: How to change attributes of child elements?

开发者 https://www.devze.com 2023-01-03 23:51 出处:网络
I have this hidden link, which is needed for other purposes: <span id=\"notitle\" style=\"display: none;\">

I have this hidden link, which is needed for other purposes:

<span id="notitle" style="display: none;">
<a href="...Foo" title="Foo" style="..."></a>
</span>

The link is generated dynamically and automatically includes the title attribute. But I'd like to remove the title attribute since the value is copied when the user copy-pastes surrounding text.

I thought of using javascript. This is what I've got so far:

<html>
    <head>
        <script type="text/ja开发者_开发问答vascript">
          function notitle() {
            var mylist=document.getElementById("notitle")
            var listitems= mylist.getElementsByTagName("a")
            for (i=0; i<listitems.length; i++) {
              listitems.setAttribute("title", "");
            }
      }
        </script>
    </head>

<body onLoad="notitle()">

<p>Before hidden link:
<span id="notitle" style="display: none;">
<a href="#Foo" title="Foo">This Link should have no title attribute</a>
</span>
After hidden link.</p>
</body>
</html>

But doesn't work. I guess it's about listitems.setAttribute("title", ""); Any idea? Cheers :)


listitems is the collection, so your code is probably throwing an error.

Anyway, you want:

listitems[i].setAttribute("title", "");


First of all, you need to select the one specific item by using listitems[i], second, I think you can do it as simple as this:

listitems[i].title = ""


You need to add the i index:

listitems[i].setAttribute("title", "");


You're doing

listitems.setAttribute("title", ""); 

i times.

Add an array index into the list.

0

精彩评论

暂无评论...
验证码 换一张
取 消