简体   繁体   English

如何根据查询从搜索结果中突出显示单词?

[英]How can I highlight words from my search result based on my query?

I would like the words i used to search for results to be highlighted in the results. 我希望我用来搜索结果的单词在结果中突出显示。 How would i go on about doing this? 我将如何进行呢?

I have looked at examples using JS but they don't seem to work with my code. 我看过使用JS的示例,但它们似乎不适用于我的代码。

html: HTML:

(Search)
<input type="text" name="q" placeholder="Search..." required  value="{{ query|escape }}" size="100" autofocus>
<input class="buttonCopy button1" type="Submit" value="Search">

(Results)
{% for resp in results %}
    <tr>
        <td style="border: 1px solid">{{ resp.Question.Statement }}</td>
        <td style="width: 70%; border: 1px solid"><div id="resp{{forloop.counter}}" style="height: 200px;overflow-y:auto;overflow-x:hidden">{{ resp|escape|linebreaks }}</div></td>

        <td><button class="buttonCopybutton3",onclick="copyFunction('text{{forloop.counter}}')">Copy</button></td>
        <td><textarea id="text{{forloop.counter}}" style="display:block; width:0; height:0; opacity:0">{{ resp }}</textarea></td>
        <td>
            <label class="container">
            <input type="checkbox" id="chck" name="responseCheck" onClick="checkbox();" value="{{ resp }}"><br></br>
            <span class="checkmark"></span></label>
        </td>
        <td><textarea id="show" name="responseCheck" style="display:block; width:0px; height:0px; opacity:0"></textarea><br></br></td>
    </tr>
{% endfor %}

views.py: views.py:

if query:
    newquery = stopwords.strip_stopwords(query)
    terms = newquery.split()
    for term in terms:
         qset &= (
            Q(Question__Statement__icontains=term) |
            Q(Response__icontains=term)
         )
    results = Response.objects.filter(qset).distinct()
else:
    results = []
    posts = ''

return render_to_response("app/search.html", {
    "results": results,
    "query": query,
    "noOfResults": len(results),
    "username": username,
    "queryR": queryR,
    "topicList": topicList,
    "clientList": clientList,
})

I'm not sure if your question is releated with python or includes using another technology. 我不确定您的问题是使用python还是包含使用其他技术。

But, I would recommend you to use: 但是,我建议您使用:

jQuery Highlight Plugin jQuery Highlight插件

Basically, you have to import jquery with: 基本上,您必须使用以下命令导入jquery

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

And the highlight script , you might download here: https://johannburkard.de/resources/Johann/jquery.highlight-5.js 以及highlight script ,您可以在这里下载: https : //johannburkard.de/resources/Johann/jquery.highlight-5.js

And once, you import te jquery highlight plugin , you can define the array of words that you need add the behavior, with: 并且,一旦导入了jquery highlight plugin ,您就可以定义添加行为的单词数组,方法是:

$("body p").highlight(["jQuery", "highlight", "plugin"]);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何突出显示搜索查询结果中的单词 - how highlight a word from result of search query 我如何从我的 sqlite 数据库中查询和获取数据,即使我的搜索输入有相似的单词彼此分开(不是连续的) - How can I query and get data from my sqlite database even when my search input have similar words that is apart from each other (not continuously) 如何从 es.search 结果中的列表中获取搜索到的字符串? - how can I get the searched string from list in my es.search result? 在 Python 中:如何让我的代码根据我的输入打印出我可以拼写的所有可能的单词? - In Python: How can i get my code to print out all the possible words I can spell based on my input? 如何将结果从 python 装饰器传递给我的类? - How can I pass the result from a python decorator to my class? 如何绘制groupby()结果? - How can I plot my groupby() result? 在 SQLite 中,如何使用输入框中的文本搜索我的数据库,并将结果显示为文本? - In SQLite, how do I search my database with text from an entry box, and display the result as text? 如何根据groupby +最大结果过滤数据框? - How can I filter my dataframe based on groupby+nlargest result? 如何防止我的网站用户发布包含脏话的帖子? - How can I prevent my site users from publishing posts that contain swear words? 如何在 html django 页面中显示我的 sqlite 查询的结果 - How can I display the result of my sqlite query in a html django page
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM