jQuery的 - 如何替换一个单词中的一个div? [英] Jquery - How to replace one word in a div?
问题描述
我有一个从其他位置复制的div,我需要更改其中的一个单词。这是html:
< div class =dealer-addy>
8726 N. Royal Ln< br>
Irving,TX 75063< br>
email@aol.com
< / div>
我需要更改电子邮件地址,所以我试图让文本在div内将字符串拆分为单词,然后更改电子邮件地址。然而,我在处理内部文本时遇到了麻烦。
这是我的脚本:
var address = $ dealerInfo.find(。dealer-addy);
var text = $(address).text();
text = text.replace(/ \ r?\\\
| \r /,);
var words = $(text).split();
它突破了split(),所以我添加了replace()行,在replace()行上。我认为这是因为'文本'中的换行符而中断,但我不知道该怎么做。 解决方案
div class =snippetdata-lang =jsdata-hide =false>
var text = $(。dealer-addy)。text();函数extractEmails(text){return text.match(/([a-zA-Z0-9 ._-] + @ [a-zA-Z0-9 ._-] + \。[a-zA-Z0- 9)._-] +)/ gi);} var email = extractEmails(text); var value = $(。dealer-addy).text(); value = value.replace(email,new @ email。 COM); $(。dealer-addy).text(value);
< script src =https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js>< / script>< div class =dealer-阿迪> 8726 N. Royal Ln< br> Irving,TX 75063< br> email@aol.com< / div>
: http://jsfiddle.net/VS7Qe/166/
I have a div that's copied from another location, and I need to change one word in it. This is the html:
<div class="dealer-addy">
8726 N. Royal Ln<br>
Irving, TX 75063<br>
email@aol.com
</div>
I need to change the email address, so I'm trying to get the text inside the div and split the string into words, then change the email address. However, I'm having trouble working with the inner text.
This is my script:
var address = $dealerInfo.find(".dealer-addy");
var text = $(address).text();
text = text.replace(/\r?\n|\r/, " ");
var words = $(text).split(" ");
It was breaking on split(), so I added the replace() line, and now it breaks on the replace() line. I assume it's breaking because of the line breaks in 'text', but I'm not sure what to do.
var text = $(".dealer-addy").text();
function extractEmails ( text ){
return text.match(/([a-zA-Z0-9._-]+@[a-zA-Z0-9._-]+\.[a-zA-Z0-9._-]+)/gi) ;
}
var email = extractEmails(text);
var value = $(".dealer-addy").text();
value = value.replace(email, "new@email.com");
$(".dealer-addy").text(value);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<div class="dealer-addy">
8726 N. Royal Ln<br>
Irving, TX 75063<br>
email@aol.com
</div>
Here is the demo: http://jsfiddle.net/VS7Qe/166/
这篇关于jQuery的 - 如何替换一个单词中的一个div?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!