[英]How do you use Xpath to find All anchor tags “In General” within a div Inner html
這個問題飼料的關閉這一個。
我只想在字符串中找到任何錨標記。 以下示例類似於上述問題。 除了我只想使用 xpath 檢測錨標記。 最好有角度。 使用 ViewChild。 接受任何使用 javascript 的解決方案。
// Our HTML source
var s = `Some Html String <a href="#">something1</a>`;
// Create a root div because XML requires a single root element
var div = document.createElement('div');
// Set the innerHTML to our string
div.innerHTML = s;
// Find any <a> tags
var iterator = document.evaluate('//a]', div, null, XPathResult.ANY_TYPE, null);
// Set the iterator
var thisNode = iterator.iterateNext();
// Loop the iterator and log the node found
while (thisNode) {
console.log(thisNode);
thisNode = iterator.iterateNext();
}
這樣對嗎?
這是你的答案。
const desc = this.description.value;
const div = document.createElement('div');
div.innerHTML = desc;
const iterator = document.evaluate('//a', div, null, XPathResult.ANY_TYPE, null);
const anchors = iterator.iterateNext();
while (anchors) {
this.toastr.error("To better improve service, external links are not permitted.");
return false;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.