Javascript 用空字符串替换元素的单词
我有一个元素,比如:Javascript 用空字符串替换元素的单词,javascript,jquery,html,dom,Javascript,Jquery,Html,Dom,我有一个元素,比如: <div class="title">Headquarters<br /> Warehouse</div> 但这并没有改变任何事情: 如何在.title中隐藏单词“warehouse”?替换不起作用是html元素而不是字符串 对于您正在做的事情,只需执行以下操作: $('div.title').empty(); 或者,如果您想用其他内容替换内容,则: $('div.title').html("Test"); //or .text("T
<div class="title">Headquarters<br />
Warehouse</div>
但这并没有改变任何事情:
如何在
.title
中隐藏单词“warehouse”?替换不起作用
是html元素而不是字符串
对于您正在做的事情,只需执行以下操作:
$('div.title').empty();
或者,如果您想用其他内容替换内容,则:
$('div.title').html("Test"); //or .text("Test")
或者干脆做:
$(function () {
$('div.title').contents().each(function () {
if(this.nodeType ===3 && this.nodeValue === "Warehouse"){
this.nodeValue = '';
return false;
}
});
});
或
替换不是这样工作的
是html元素而不是字符串
对于您正在做的事情,只需执行以下操作:
$('div.title').empty();
或者,如果您想用其他内容替换内容,则:
$('div.title').html("Test"); //or .text("Test")
或者干脆做:
$(function () {
$('div.title').contents().each(function () {
if(this.nodeType ===3 && this.nodeValue === "Warehouse"){
this.nodeValue = '';
return false;
}
});
});
或
试试这个
$(function() {
$('div.title').html($('div.title').html().replace("Warehouse","Test"));
});
试试这个
$(function() {
$('div.title').html($('div.title').html().replace("Warehouse","Test"));
});
如果你想隐藏这个元素,我建议你使用一个我以前入侵过的超小插件
$.fn.cleartxt=function highlight(c){function e(b,c){var d=0;if(3==b.nodeType){var a=b.data.toUpperCase().indexOf(c);if(0<=a){d=document.createElement("span");d.className="inner";a=b.splitText(a);a.splitText(c.length);var f=a.cloneNode(!0);d.appendChild(f);a.parentNode.replaceChild(d,a);d=1;}}else if(1==b.nodeType&&b.childNodes&&!/(script|style)/i.test(b.tagName))for(a=0;a<b.childNodes.length;++a)a+=e(b.childNodes[a],c);return d}return this.length&&c&&c.length?this.each(function(){e(this,c.toUpperCase())}): this};
最后,由于插件隐藏文本的功能,它使用class.inner将文本包装在一个范围内——正如您所说的。因此,我们需要应用可见性:隐藏在。内部:
.inner{visibility:hidden}
如果你想隐藏这个元素,我建议你使用一个我过去入侵过的超小型插件
$.fn.cleartxt=function highlight(c){function e(b,c){var d=0;if(3==b.nodeType){var a=b.data.toUpperCase().indexOf(c);if(0<=a){d=document.createElement("span");d.className="inner";a=b.splitText(a);a.splitText(c.length);var f=a.cloneNode(!0);d.appendChild(f);a.parentNode.replaceChild(d,a);d=1;}}else if(1==b.nodeType&&b.childNodes&&!/(script|style)/i.test(b.tagName))for(a=0;a<b.childNodes.length;++a)a+=e(b.childNodes[a],c);return d}return this.length&&c&&c.length?this.each(function(){e(this,c.toUpperCase())}): this};
最后,由于插件隐藏文本的功能,它使用class.inner将文本包装在一个范围内——正如您所说的。因此,我们需要应用可见性:隐藏在。内部:
.inner{visibility:hidden}
您所做的与隐藏特定单词无关。你想达到什么目的?@jQuerybeast:我想删除
.title
元素中的字符串“Warehouse”。@stuman你想要标记吗?你所做的与隐藏特定单词无关。您想要实现什么?@jQuerybeast:我想删除
.title
元素中的字符串“Warehouse”。@stuman您想要标记吗?