Javascript 如何将html附加到选定元素后第一个出现的字符串

Javascript 如何将html附加到选定元素后第一个出现的字符串,javascript,jquery,jquery-selectors,Javascript,Jquery,Jquery Selectors,我有这样的html: <div class=foo> (<a href=forum.example.com>forum</a>) <p> Some html here.... </div> …这将把它放在)前面 那么,我如何扩展jQuery选择以选择文字),或者我需要使用其他解决方案吗?您是否可以选择下一个标记并在链接前加上前缀(实际上…使用该方法) $('div.foo a:first').next('<p>

我有这样的html:

<div class=foo>
  (<a href=forum.example.com>forum</a>)
  <p>
  Some html here....
</div>
…这将把它放在
前面


那么,我如何扩展jQuery选择以选择文字
,或者我需要使用其他解决方案吗?

您是否可以选择下一个
标记并在链接前加上前缀(实际上…使用该方法)

$('div.foo a:first').next('<p>').before(' <a href=blog.example.com>blog</a>');
$('div.foo a:first')。next('p>')。before('');

您是否可以选择下一个
标记并在链接前加上前缀(实际上…使用该方法)

$('div.foo a:first').next('<p>').before(' <a href=blog.example.com>blog</a>');
$('div.foo a:first')。next('p>')。before('');

您可以匹配并替换整个
()

Append还在现有锚点内插入新的锚点标记。您可能会发现您正在寻找

您可以匹配并替换整个
()

Append还在现有锚点内插入新的锚点标记。您可能会发现您正在寻找的是

一个永远不应该通过正则表达式修改XML/HTML(尤其是不可信的/格式错误的),但是如果您真的非常绝望:

var container = $('div.foo');
var append = '<a href="...">...</a>';
var html = container.html();
var replaced = html.replace(/\(<a [^\)]+\)/, "$& " + append);
container.html(replaced);
var容器=$('div.foo');
var-append='';
var html=container.html();

var replaced=html.replace(/\(不应该用正则表达式修改XML/html(尤其是不可信的/格式错误的),但是如果您真的非常绝望:

var container = $('div.foo');
var append = '<a href="...">...</a>';
var html = container.html();
var replaced = html.replace(/\(<a [^\)]+\)/, "$& " + append);
container.html(replaced);
var容器=$('div.foo');
var-append='';
var html=container.html();
var replaced=html.replace(/\(
var container = $('div.foo');
var append = '<a href="...">...</a>';
var html = container.html();
var replaced = html.replace(/\(<a [^\)]+\)/, "$& " + append);
container.html(replaced);