我試圖從HTML <body>
字符串獲取所有內聯事件標記的列表,我如何能夠做到這一點?從HTML字符串獲取所有內聯事件
例子:
<a onclick='foo()'></a>
我想提取onclick='foo()'
。
REGEX或其他選擇可能嗎?
我試圖從HTML <body>
字符串獲取所有內聯事件標記的列表,我如何能夠做到這一點?從HTML字符串獲取所有內聯事件
例子:
<a onclick='foo()'></a>
我想提取onclick='foo()'
。
REGEX或其他選擇可能嗎?
你應該讓瀏覽器做解析,比如像這樣:
var attributes = Array.prototype.slice.call(doc.body.attributes);
for (var i = 0; i < attributes.length; i++) {
if (/^on/.test(attributes[i].name)) {
console.log(attributes[i].name, attributes[i].value);
}
}
:
var doc = document.implementation.createHTMLDocument('');
doc.documentElement.innerHTML = '<body onload="alert(1)"></body>'; // your string here
然後使用DOM方法得到on*
屬性
yes ** [請看這裏](http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags/1732454#1732454)** – qwertymk
是,我讀過,想知道如果任何替代品都可以。 雖然內聯方面的事件非常穩定,所以REGEX可能會起作用。 – Trevor
你可以遍歷每個元素並訪問它的'onXXXXX'屬性... –