我目前正在 Blackbaud/Convio CRM 工作,我有一个关于使用 Javascript 或 jQuery 隐藏从后端渲染到 DOM 上的元素的问题。下面是从后端渲染到 DOM 上的代码:
<div id="wrapperBlock">
<div class="topList">TOP COLORS</div>
1 -
<a href="http://rcf.convio.net/site/TR?company_id=1025&fr_id=1341&pg=company">Orange</a>
($100)
<br />
2 -
<a href="http://rcf.convio.net/site/TR?company_id=1022&fr_id=1341&pg=company">Black</a>
($80)
<br />
3 -
<a href="http://rcf.convio.net/site/TR?company_id=1026&fr_id=1341&pg=company">Green</a>
($75)
<br />
4 -
<a href="http://rcf.convio.net/site/TR?company_id=1024&fr_id=1341&pg=company">Yellow</a>
($68)
<br />
5 -
<a href="http://rcf.convio.net/site/TR?company_id=1020&fr_id=1341&pg=company">Blue</a>
($55)
<br />
6 -
<a href="http://rcf.convio.net/site/TR?company_id=1027&fr_id=1341&pg=company">Red</a>
($43)
<br />
7 -
<a href="http://rcf.convio.net/site/TR?company_id=1021&fr_id=1341&pg=company">Purple</a>
($30)
<br />
8 -
<a href="http://rcf.convio.net/site/TR?company_id=1023&fr_id=1341&pg=company">Pink</a>
($47,546.81)
<br />
</div>
页面上的输出如下所示:
1 - Orange ($100)
2 - Black ($80)
3 - Green ($75)
4 - Yellow ($68)
5 - Blue ($55)
6 - Red ($43)
7 - Purple ($30)
8 - Pink ($20)
我想隐藏每种颜色之前出现的数字、连字符和空格。此外,是否可以隐藏包含每个美元金额的左括号和右括号?
要删除“1 - ”,我尝试了以下操作:
<script type="text/javascript">
$(document).ready(function(){
$('#progress-companies').html().remove(' 1 - ');
});
</script>
有人可以给我一个关于如何去掉数字、破折号和括号的提示吗?我需要正则表达式吗?
编辑/更新:
哦,我在某个地方使用了 @Sangeet-Menon 解决方案中的一行代码 Hide a character in text with jQuery?
$('#progress-companies').html($('#progress-companies').html().replace(" 1 - ",""));
});
最佳答案
假设您只想保留 <a>
元素,据我所知,我建议避免使用正则表达式1,只需:
// iterate over each of the '<a>' elements inside of the '#wrapperBlock':
$('#wrapperBlock a').each(function(){
// caching the current node:
var self = this;
// removing the current node's previousSibling, the textNode
// containing the characters you want to remove, from the parentNode:
self.parentNode.removeChild(self.previousSibling);
// removing the parentheses from the current node's nextSibling
// (this is text, not HTML) using a regular expression and String.replace():
self.nextSibling.nodeValue = self.nextSibling.nodeValue.replace(/\(|\)/g,'');
});
$('#wrapperBlock a').each(function(){
var self = this;
self.parentNode.removeChild(self.previousSibling);
self.nextSibling.nodeValue = self.nextSibling.nodeValue.replace(/\(|\)/g,'');
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="wrapperBlock">
<div class="topList">TOP COLORS</div>
1 -
<a href="http://rcf.convio.net/site/TR?company_id=1025&fr_id=1341&pg=company">Orange</a>
($100)
<br /> 2 -
<a href="http://rcf.convio.net/site/TR?company_id=1022&fr_id=1341&pg=company">Black</a>
($80)
<br /> 3 -
<a href="http://rcf.convio.net/site/TR?company_id=1026&fr_id=1341&pg=company">Green</a>
($75)
<br /> 4 -
<a href="http://rcf.convio.net/site/TR?company_id=1024&fr_id=1341&pg=company">Yellow</a>
($68)
<br /> 5 -
<a href="http://rcf.convio.net/site/TR?company_id=1020&fr_id=1341&pg=company">Blue</a>
($55)
<br /> 6 -
<a href="http://rcf.convio.net/site/TR?company_id=1027&fr_id=1341&pg=company">Red</a>
($43)
<br /> 7 -
<a href="http://rcf.convio.net/site/TR?company_id=1021&fr_id=1341&pg=company">Purple</a>
($30)
<br /> 8 -
<a href="http://rcf.convio.net/site/TR?company_id=1023&fr_id=1341&pg=company">Pink</a>
($47,546.81)
<br />
</div>
- 主要是因为操作和重置 HTML(使用 jQuery 的
html()
或 native DOM 的innerHTML
)将取消设置分配给您正在操作的 HTML 内部节点的任何事件处理程序,并且将要么需要重新绑定(bind),要么需要委托(delegate)事件处理(例如使用on()
)。
引用文献:
-
JavaScript:
- JavaScript Regular Expressions .
-
Node.parentNode
. -
Node.nextSibling
. -
Node.nodeValue
. -
Node.previousSibling
. -
Node.removeChild()
. -
String.prototype.replace()
. - jQuery:
-
each()
.
-
关于javascript - 如何使用 jQuery/Javascript 隐藏 anchor 标记之前的元素?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26470016/