jquery部分删除文本
我可以使用jQuery删除div中的部分文本。
像这样:
Published May 18th 2011 - Approuved - Expire May 18 th 2012
Source: SuperSite
我想删除Approuved – Expire May 18th 2012
结果将是:
Published May 18th 2011
Source: SuperSite
你可以使用jquery来选择你的元素/ html,但javascript有一个内置函数replace
,可以满足你的需要:
$('div.entry').html($('div.entry') .html() .replace('Approuved - Expire May 18 th 2012', ''))
或者使用RegExp
:
如果要替换的内容始终使用Approuved - Expire
启动Approuved - Expire
:
$('div.entry').html($('div.entry') .html() .replace(/Approuved - Expire[^<]*/, ''))
试试这个:
$(".entry").html(function(i, htm){ return htm.split("-")[0]; });
如果要在节点内处理html然后重置它,html()也可以传递一个函数。
这是关于jsfiddle的演示: http : //jsfiddle.net/nKJWn/
$(".entry").html(($(".entry").html().replace("Approuved - Expire May 18 th 2012","")))