[英]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.