Javascript jquery在同一DOM中的文本之前插入

Javascript jquery在同一DOM中的文本之前插入,javascript,jquery,Javascript,Jquery,嗨,我正在努力向这个DOM插入一个文本,但是我的尝试没有像我预期的那样成功 <p class="doing">Peter are <a href="mysite.com">here</a></p> <script>$("p.doing").before("I and ");</script> 彼得是谁 美元(“p.doing”)。在(“我和”); 我希望有结果: <p class="doing">I and

嗨,我正在努力向这个DOM插入一个文本,但是我的尝试没有像我预期的那样成功

 <p class="doing">Peter are <a href="mysite.com">here</a></p>
<script>$("p.doing").before("I and ");</script>
彼得是谁

美元(“p.doing”)。在(“我和”); 我希望有结果:

<p class="doing">I and Peter are <a href="mysite.com">here</a></p>

我和彼得是

但事实是:

I and 
<p class="doing">Peter are <a href="mysite.com">here</a></p>
I和
彼得是谁


请告知如何解决此问题。

请改用prepend

$(".doing").html('I and '+$(".doing").html());
$("p.doing").prepend("I and ");

改为使用前置

$("p.doing").prepend("I and ");
使用jQuery方法:

彼得是谁

$(“.doing”)。前置(“I和”); 使用jQuery方法:

彼得是谁

$(“.doing”)。前置(“I和”);

您应该使用prepend,而不是before,因为在类doing中将文本插入到p标记中

<!DOCTYPE html>
<html>
<head>

</head>
<body>
  <p class="doing">Peter are <a href="mysite.com">here</a></p>
<script>$(".doing").prepend("I and");</script>
</body>
</html>​

彼得是谁

$(“.doing”)。前置(“I和”); ​
您应该使用prepend,而不是before,因为在类doing中将文本插入到p标记中

<!DOCTYPE html>
<html>
<head>

</head>
<body>
  <p class="doing">Peter are <a href="mysite.com">here</a></p>
<script>$(".doing").prepend("I and");</script>
</body>
</html>​

彼得是谁

$(“.doing”)。前置(“I和”); ​
这将在
标记之前添加“I和”文本-Thanaborn希望它包含在标记中。@AlexKalicki应该使用Anthony所说的prepend。感谢您纠正我的错误,我将编辑我的答案。这将在
标记之前添加“i和”文本-Thanaborn希望它包含在标记中。@AlexKalicki应该使用Anthony提供的prepend。谢谢你帮我更正,我会编辑我的答案。嗨,安东尼,这对我来说很有用,非常感谢你做出的巨大贡献。嗨,安东尼,这对我来说很有用,非常感谢你做出的巨大贡献…实际上正确的顺序是'彼得和我':
$('p.doing').html($('p.doing').html().replace('Peter','Peter and i')。如果你关心语法:)…实际上正确的顺序是“彼得和我”:
$('p.doing').html($('p.doing').html().replace('Peter','Peter和I')。如果你关心语法:)