admin管理员组

文章数量:1266931

I want to pleately wipe the title attribute from all elements inside a html doc. From table, p, img, div etc..

Currently I do this:

$(document).ready(function(){
$("a").removeAttr("title");
$("img").removeAttr("title");
$("div").removeAttr("title");
// and so on
// and so on
// and so on
});

Is there a more elegant way to do this? Without selecting individual elements?

I want to pleately wipe the title attribute from all elements inside a html doc. From table, p, img, div etc..

Currently I do this:

$(document).ready(function(){
$("a").removeAttr("title");
$("img").removeAttr("title");
$("div").removeAttr("title");
// and so on
// and so on
// and so on
});

Is there a more elegant way to do this? Without selecting individual elements?

Share Improve this question asked Aug 22, 2016 at 14:01 MalasorteMalasorte 1,1737 gold badges23 silver badges47 bronze badges 3
  • 1 But why would you? – Rudie Visser Commented Aug 22, 2016 at 14:03
  • When HTML is loaded on a touch screen device I just want the title to go away. – Malasorte Commented Aug 22, 2016 at 14:04
  • $('[title]').removeAttr('title'); You could use the attribute selector so that it just loads elements with the attribute of title instead of every element on your page. api.jquery./has-attribute-selector – Sean Wessell Commented Aug 22, 2016 at 14:11
Add a ment  | 

4 Answers 4

Reset to default 5

Use the attribute selector and select just the elements with the title attribute and not all elements.

$("[title]").removeAttr("title");

The all selector, *, selector should do the trick

$('*').removeAttr('title');

You can simply do this using All Selector (“*”):

$("*").removeAttr("title");

Without jQuery this would be:

Array.from(document.getElementsByTagName('*')).forEach(elem => elem.removeAttribute('title'));

or e.g. to remove the attribute only from specific tags, e.g. img:

Array.from(document.getElementsByTagName('img')).forEach(elem => elem.removeAttribute('title'));

本文标签: javascriptRemove attribute title from all HTML elementsStack Overflow