2
IST有什麼更好的辦法來替代/包的H TP://name.tld/request_url參數或H TPS://name.tld/request_url在一定的HTML元素參數文本與<a href>
jQuery:用<a href="this">...</a>替換()或包裝()http://name.tld/request_url?參數?
HTML
<div id="posts">
<div class="post">
Tweet text 1 http://google.com and other text.
</div>
<div class="post">
Tweet text 2 https://www.google.com and other text.
</div>
<div class="post">
Tweet text 3
</div>
<div class="post">
...
</div>
</div>
的JavaScript
jQuery('#posts .post').each(function() {
elem = jQuery(this);
elem.html(
elem.text()
.replace(/(http?:\/\/?\S+)/g, "<a href='$1'>$1</a>")
);
});
任何jQuery.wrap()替代將是不錯太...
你是對的 - wrap()對純文本不起作用, 如果節點內可能有其他HTML代碼,則不應使用text()。 我的問題只是,是否有任何方法可以用較少的代碼/ efford來做到這一點?代碼可以更早地被操縱。服務器端或AJAX調用的響應成功函數。 Thx爲您的答案無論如何... – gabel 2010-09-01 12:58:40
@gabel - 沒有真正的更便宜的方式,我見過,除非你提前做服務器端(我會)。 – 2010-09-01 13:07:59