[英]Modifying plain text inside div without modifying the inner span
I have div nodes like我有像这样的 div 节点
<div> Text <span class="required">*</span></div>
and和
<div> Text </div>
I want to convert them into我想将它们转换为
<div> <a href="some target">Text</a><span class="required">*</span></div>
and和
<div> <a href="some target">Text</a></div>
How I achieve this using JavaScript (regex?) or jQuery我如何使用 JavaScript (正则表达式?)或 jQuery 来实现这一点
Maybe someone will have a better solution (I hope and look forward to)也许有人会有更好的解决方案(我希望并期待)
Provided you know your structure is going to be like that, this should work just fine:如果您知道您的结构将是这样的,这应该可以正常工作:
$(function(){
var html = $('div').html();
var fixed = html.substring(html.indexOf('<span'), html.length);
var toWrap = html.substring(0, html.indexOf('<span'));
var newHtml = '<a href="sometarget">'+toWrap+'</a>'+fixed;
$('div').html(newHtml);
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.