jQuery 将 html 添加到结果中

发布于 2024-09-24 02:24:33 字数 1169 浏览 0 评论 0原文

我的输入数组有:

results[num_row] = {
    'title': title,
    'url': url,
    'support_url': support_url,
    'description': description,
    'contacts': contacts
};

我得到结果:

function formatItem(item){
   var highlight = $.Autocompleter.defaults.highlight;
   var temp = '<span class="title">' + highlight(item.title, term)  + '</span>';
   temp += '<br />&nbsp;&nbsp;' + item.description;
   return temp;
} 

function prep(){
$("#searchbox").autocomplete(results,{
    width:500,
    scroll:false,
    formatItem: formatItem,
    highlight: false

}).result(function(event, item) {
    location.href = item.url;
});
}

我希望能够向返回的内容添加标签,以便我可以使用 css 覆盖颜色。例如,我想做类似的事情:

formatItem: function(item) {
    var temp = '<span class="title">' + item.title + </span> + '<br />&nbsp;&nbsp;' + <span class="description"> + item.description + </span>;
    return temp;
}

当我尝试像这样添加内联标签时,它会将输入搜索条件更改为具有该文字标签。因此,我必须实际输入 Search String 才能进行搜索,而不是 Search String

谢谢。

my input array has:

results[num_row] = {
    'title': title,
    'url': url,
    'support_url': support_url,
    'description': description,
    'contacts': contacts
};

I get the results back:

function formatItem(item){
   var highlight = $.Autocompleter.defaults.highlight;
   var temp = '<span class="title">' + highlight(item.title, term)  + '</span>';
   temp += '<br />  ' + item.description;
   return temp;
} 

function prep(){
$("#searchbox").autocomplete(results,{
    width:500,
    scroll:false,
    formatItem: formatItem,
    highlight: false

}).result(function(event, item) {
    location.href = item.url;
});
}

I'd like to be able to add tags to what is being returned so that I can override the colors using css. For example I'd like to do something like:

formatItem: function(item) {
    var temp = '<span class="title">' + item.title + </span> + '<br />  ' + <span class="description"> + item.description + </span>;
    return temp;
}

When I try adding the tags inline like that, it changes the input search critera to having that literal tag. So I have to actually type <span class="title">Search String in order to search instead of Search String.

Thank you.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

旧街凉风 2024-10-01 02:24:33

之后您的函数将通过 options.highlight 运行。尝试指定返回源字符串的突出显示函数:

$("#searchbox").autocomplete(results, {
    formatItem: function(item, foo, bar, term) {
        var highlight = $.Autocompleter.defaults.highlight;
        var temp = '<span class="title">' + highlight(item.title, term)  + '</span>';
        temp += '<br />  ' + item.description;

        return temp;
    },

    highlight: false
}).result(function(event, item) {
    location.href = item.url;
});

确保转义 item.title 以防止 XSS 攻击!

Your function is being run through options.highlight afterwards. Try specifying a highlight function which returns the source string:

$("#searchbox").autocomplete(results, {
    formatItem: function(item, foo, bar, term) {
        var highlight = $.Autocompleter.defaults.highlight;
        var temp = '<span class="title">' + highlight(item.title, term)  + '</span>';
        temp += '<br />  ' + item.description;

        return temp;
    },

    highlight: false
}).result(function(event, item) {
    location.href = item.url;
});

Be sure you're escaping item.title to prevent XSS attacks!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文