从html页面中使用javascript删除html标签 [英] remove the html tag using javascript from html page

查看:93
本文介绍了从html页面中使用javascript删除html标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经写了一个javascript codefor autosuggest。我得到autosuggest.but但当我选择任何autosuggested word.it与这样的html标签来:

如果我在文本框中键入w。然后我得到一些自动提示的单词,如果我选择那个单词,然后在文本框中它就像 w 水, w 所有其他。


$
$ b

 < html> 
< head>
< / head>
< body>
< input name =txttype =textid =new
placeholder =请输入一些文字。>
< script type =text / javascriptsrc =autoComplt.js>< / script>
< script>
var words = [];
函数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(文字);
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标记。

 函数stripHTML(html){
var div = document.createElement ( DIV);
div.innerHTML = html;
返回div.textContent || div.innerText;
}

演示: http://jsfiddle.net/jfriend00/tbnfnvv6/


i have written a javascript codefor autosuggest. i am getting autosuggest.but when i am selecting any autosuggested word.it's coming with html tag like this.:

if i am typing "w" in text box. then i am getting some autosuggested word if i select that word then on the text box it coming like w water,wall etc.

below is my code.:

<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>

how can i remove that html tag from the textbox.

解决方案

You can convert an HTML string to text by inserting it into a DOM element and then asking for just the text back out. This will work in even older versions of IE:

function stripHTML(html) {
    var div = document.createElement("div");
    div.innerHTML = html;
    return div.textContent || div.innerText;
}

Working demo: http://jsfiddle.net/jfriend00/tbnfnvv6/

这篇关于从html页面中使用javascript删除html标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆