我基本上需要突出显示一个文本块中的特定单词.例如,假装我想在本文中突出显示"dolor"这个词:
<p>
Lorem ipsum dolor sit amet, consectetuer adipiscing elit.
</p>
<p>
Quisque bibendum sem ut lacus. Integer dolor ullamcorper libero.
Aliquam rhoncus eros at augue. Suspendisse vitae mauris.
</p>
Run Code Online (Sandbox Code Playgroud)
如何将上述内容转换为以下内容:
<p>
Lorem ipsum <span class="myClass">dolor</span> sit amet, consectetuer adipiscing elit.
</p>
<p>
Quisque bibendum sem ut lacus. Integer <span class="myClass">dolor</span> ullamcorper
libero. Aliquam rhoncus eros at augue. Suspendisse vitae mauris.
</p>
Run Code Online (Sandbox Code Playgroud)
这有可能与jQuery?
编辑:正如Sebastian 指出的那样,没有jQuery这很有可能 - 但是我希望有一个特殊的jQuery方法可以让你在文本本身上做选择器.我已经在这个网站上大量使用jQuery了,所以把所有内容都包含在jQuery中会让事情变得更加整洁.
我有这个:
<div class="" id="fancy_hover">
<a href="home.html" id="start_writer"></a>
<div>
<p class="hidden" style="display: block;">click here to see this event with all the bells and whistles</p>
</div>
</div>
Run Code Online (Sandbox Code Playgroud)
我需要做一个等效的.find()但是对于字符串"click here"而不是节点,我需要将它包装在一个<a>标签中.
什么是jquery的最佳方法?
我有以下代码在FF/Chrome中工作
var stack = [Array.prototype.slice.call(document.getElementsByTagName("body")[0].childNodes)], nodes, node, parent, text, offset;
while (stack.length) {
nodes = stack.pop();
for (var i=0, n=nodes.length; i<n; ++i) {
node = nodes[i];
switch (node.nodeType) {
case Node.ELEMENT_NODE:
if (node.nodeName.toUpperCase() !== "SCRIPT") {
stack.push(Array.prototype.slice.call(node.childNodes));
}
break;
case Node.TEXT_NODE:
text = node.nodeValue;
offset = text.indexOf("[test=");
if (offset >= 0 && text.substr(offset).match(/^(\[test=(\d+)\])/)) {
parent = node.parentNode;
var before = document.createTextNode(text.substr(0, offset));
link = document.createElement("a"),
after = document.createTextNode(text.substr(offset + RegExp.$1.length));
link.appendChild(document.createTextNode(text.substr(offset, RegExp.$1.length)));
link.setAttribute("href", "http://example.com/" + RegExp.$2);
parent.insertBefore(after, node);
parent.insertBefore(link, after); …Run Code Online (Sandbox Code Playgroud) 我试图通过<b>在匹配的子字符串周围插入标签来突出显示字符串中的匹配项.例如,如果查询是"cat",则:
"I have a cat."
Run Code Online (Sandbox Code Playgroud)
应成为:
"I have a <b>cat</b>."
Run Code Online (Sandbox Code Playgroud)
同样,如果查询是"堆栈溢出",则:
"Stack Overflow is great."
Run Code Online (Sandbox Code Playgroud)
应成为:
"<b>Stack Overflow</b> is great."
Run Code Online (Sandbox Code Playgroud)
换句话说,我必须保留原始字符串的大小写,但在匹配时不区分大小写.
到目前为止我尝试的一件事是:
var regex = new RegExp('(' + query + ')', 'i');
return strResult.replace(regex, '<b>$1</b>');
Run Code Online (Sandbox Code Playgroud)
但是,如果查询中有任何括号,这会导致运行时异常,并且我认为尝试转义所有可能的正则表达式字符会太麻烦.
我有一些纯文本和HTML.我需要创建一个PHP方法,它将返回相同的html,但<span class="marked">在文本的任何实例之前和</span>之后.
请注意,它应该支持html中的标记(例如,如果文本是blabla这样的话,它应该标记为何时bla<b>bla</b>或<a href="http://abc.com">bla</a>bla.
它应该是敏感的并且支持长文本(使用多行等).
例如,如果我使用文本"我的名字是josh"和以下html调用此函数:
<html>
<head>
<title>My Name Is Josh!!!</title>
</head>
<body>
<h1>my name is <b>josh</b></h1>
<div>
<a href="http://www.names.com">my name</a> is josh
</div>
<u>my</u> <i>name</i> <b>is</b> <span style="font-family: Tahoma;">Josh</span>.
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
......它应该返回:
<html>
<head>
<title><span class="marked">My Name Is Josh</span>!!!</title>
</head>
<body>
<h1><span class="marked">my name is <b>josh</b></span></h1>
<div>
<span class="marked"><a href="http://www.names.com">my name</a> is josh</span>
</div>
<span class="marked"><u>my</u> <i>name</i> <b>is</b> <span style="font-family: Tahoma;">Josh</span></span>.
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
谢谢.
可能重复:
用Javascript/jQuery中的正则表达式替换模式的所有实例
如何使用jQuery来设置特定单词的所有实例的样式/部分/?
说我有代码:
<div class="replace">
<i>Blah</i>
<u>Blah</u>
</div>
Run Code Online (Sandbox Code Playgroud)
现在,我想<用其他东西替换div中的所有内容.
如果我使用$('#div').html().replace('<','somethingElse');它只替换第一个实例.
如何替换所有实例?
谢谢
我有一个HTML页面,其中包含英语单词和波斯语单词.我想知道如何检测英语单词并改变它们的颜色,并检测波斯语单词并将它们的颜色更改为不同的颜色.
这些词是动态的,可以改变.我想通过jQuery或JavaScript检测它们并更改颜色.
例如,给定此文本:
Hi ???? ?? this ??? text can be ???? ????? ...
Run Code Online (Sandbox Code Playgroud)
我想用红色显示这些单词:
Hi, This, Text, can, be,
Run Code Online (Sandbox Code Playgroud)
这些字是蓝色的:
??, ????, ???, ????, ?????
Run Code Online (Sandbox Code Playgroud)