所以我目前正在使用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>
有人可以给我一个关于如何摆脱数字,破折号和圆括号的提示吗?我需要正则表达式吗?
EDIT / UPDATE:
哦,我在@ Sangeet-Menon的解决方案中使用了一行代码Hide a character in text with jQuery?
$('#progress-companies').html($('#progress-companies').html().replace(" 1 - ",""));
});
答案 0 :(得分:2)
您可以将.replace()
用于其他数字。
$(document).ready(function(){
$('#wrapperBlock').html(function (i, oldHTML){
return oldHTML.replace(/ [0-9] - /g, '');
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.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>
答案 1 :(得分:2)
鉴于您只想保留<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()
或本机DOM的innerHTML
)将取消设置分配给HTML内部节点的任何事件处理程序操纵,并且需要重新绑定,或者需要委托事件处理(例如,使用on()
)。参考文献: