如何从给定的HTML文本只使用jQuery的更换? [英] How to get only text from given HTML and Replace using Jquery?

查看:221
本文介绍了如何从给定的HTML文本只使用jQuery的更换?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在jQuery的帮助。先谢谢了。

I need help in jquery. Thanks in advance.

我使用jquery的自动完成功能。这里是一个亮点选项。像: -

I am using autocomplete function of jquery. There is a option for highlight. as like :-

highlight: function(match, keywords) {
            return match.replace(new RegExp("("+keywords+")", "gi"),'<b>$1</b>');
        }

这是工作的罚款。但我在HTML表单中取得的结果。像: -

It is working fine.. But i am getting result in HTML form. as like :-

<div id='22048,63' style='width:200px;text-align:left;'><div class='floatleft' style='width:165px'>(LAS) Las Vegas McCarran International Airport, Las Vegas, Nevada</div><div class='floatright' style='width:20px;padding-right:15px;'><img src='images/plane.gif' width='14' height='16' /></div></div>

这就是为什么如果我在文本框中输入LAS比它是由HTML,比我的设计取代了阶级属性已捣碎。由于LAS关键字出现在类出现。因此,它正在取代这一点。

Thats why if i enter in textbox "las" than it is replacing the class attribute from HTML and than my design has mashed up. Because "las" keyword is appearing in "Class". So it is replacing this.

我只想替换文本,我在HTML中得到。

I want only replace text that i got in HTML.

不是告诉我有jQuery中的任何选项,这样我可以做到这一点。

Than tell me there is any option in jquery so i can do this..

感谢和放大器;问候

推荐答案

您可以使用的.text()函数来获取只是文本没有HTML标记。例如:

You can use .text() function to get just the text without the HTML Tags. Example:

function removeAllHtmlInsideDiv() {
    $(".example_1").html( $(".example_1").text() );
};
removeAllHtmlInsideDiv();

在你的情况下,它可以被用作

In your case, it can be used as:

highlight: function(match, keywords) {
    return (match.replace(new RegExp("("+keywords+")", "gi"),'<b>$1</b>')).text();
}

为了从字符串剥离HTML标签:

function stripHTML(html)
{
    // var html = "<p>Some HTML</p>";
    var div = document.createElement("div");
    div.innerHTML = html;
    var text = div.textContent || div.innerText || "";
    return text;
}

所以你的情况,你需要把这种方式:

So in your case, you need to put this way:

stripHTML(highlight);

希望它帮助! :)

Hope it helps! :)

这篇关于如何从给定的HTML文本只使用jQuery的更换?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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