![](/img/trans.png)
[英]Disable the javascript and meta tag from other HTML page by using Javascript
[英]remove the html tag using javascript from html page
我已经为自动建议编写了一个javascript代码。 我正在自动建议。但是当我选择任何自动建议的单词时,它会带有这样的html标签:
如果我在文本框中输入“ w”。 那么我会得到一些自动建议的单词,如果我选择了那个单词,那么在文本框中它就会像w water, w all等。
下面是我的代码:
<html>
<head>
</head>
<body>
<input name="txt" type="text" id="new"
placeholder="Please key in some text.">
<script type="text/javascript" src="autoComplt.js"></script>
<script>
var words =[];
function get_words(q,calbk)
{
var xmlhttp = new XMLHttpRequest();
var url = "http://xxxx.poc.yyyyy.com/v1/xxx/suggest?authKey=baef7f8e39c53t6990f852c8a14b7f6018b58&q="
+ q + "&rows=8";
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
var data = JSON.parse(xmlhttp.responseText);
var items = data.suggestions;
var words = items.map(function(item){return item.suggestion;});
console.log(words);
calbk(words)
}
}
xmlhttp.open("GET", url, true);
xmlhttp.send();
}
var input = document.querySelector("input[name=txt]");
autoComplt.enable(input, {
hintsFetcher : function(v, openList) {
console.log("in")
get_words(v,openList)
}
});
</script>
</body>
</html>
我如何从文本框中删除该html标签。
您可以将HTML字符串转换为文本,方法是将其插入DOM元素,然后仅索取文本。 这将在IE的较旧版本中运行:
function stripHTML(html) {
var div = document.createElement("div");
div.innerHTML = html;
return div.textContent || div.innerText;
}
您可以使用DOMParser API:并查找返回结果的textContent:
var words = ["<b>w</b>hite out", "<b>w</b>ireless mouse", "<b>w</b>hite board", "<b>w</b>ater", "<b>w</b>ireless keyboard", "<b>w</b>ireless router", "<b>w</b>ireless printers", "<b>w</b>indow envelopes"] var p = new DOMParser().parseFromString(words[Math.floor(Math.random()*words.length)], 'text/html').documentElement.textContent; document.body.textContent = p;
( ps :通过单击clbk()
函数中的一个来更改Math.floor(Math.random()*words.length)
。)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.