python - 如何根据我的查询突出显示搜索结果中的单词?
问题描述
我希望我用来搜索结果的单词在结果中突出显示。我将如何继续这样做?
我查看了使用 JS 的示例,但它们似乎不适用于我的代码。
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 %}
视图.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,
})
解决方案
我不确定您的问题是否与使用其他技术有关python
或包括使用其他技术。
但是,我建议您使用:
基本上,您必须使用以下命令导入jquery
:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
你highlight script
可以在这里下载:
https ://johannburkard.de/resources/Johann/jquery.highlight-5.js
一旦导入 te jquery highlight plugin
,您就可以定义添加行为所需的单词数组,其中:
$("body p").highlight(["jQuery", "highlight", "plugin"]);
推荐阅读
- python - 打印带星号的垂直直方图
- python - 无论如何要预先确定tkinter中的行和列吗?
- bash - 仅当未设置 VERBOSE 时才将输出重定向到 /dev/null
- c# - System.Text.Json JsonSerializer.Deserialize
(...) 无法反序列化 object[] 类型 - java - 如何将 Arraylist 保存并打印到文件中?
- node.js - WordPress 和 React - 前端框架内部服务器错误
- rust - 如何从一组字符串中提取一个值?
- wordpress - 创建产品类别 woocommerce 时出现红色未定义消息
- python - 使用字符串切片替换字符串中所有出现的子字符串
- python - 如何在没有硬编码引用的情况下调用字典键,例如 ie。if/else 语句(带有条目/输入)?