2014-02-17 63 views
1

這並不理想,但我試圖在我擁有的標題周圍封裝一些HTML。有可能在文本中定位標題文本幷包裝HTML?圍繞文字環繞HTML

開始文本:

<div class="header-intro-text"> 
    <h1>New Business Conference</h1> 
</div> 

結束文本添加了HTML

<div class="header-intro-text"> 
    <h1><a href="#">New Business Conference</a></h1> 
</div> 
+0

肯定。您可以嘗試使用'$('。header-intro-text')'的innerHTML並對其進行查找替換,然後替換innerHTML –

回答

4

嘗試:

$(".header-intro-text h1").wrapInner('<a href="#"></a>'); 
+0

錯誤的方法:-( – qwertynl

+0

@qwertynl您是對的。 –

+1

我更喜歡這個:)我正在使用手動方法。學過的知識。需要改進jQuery-fu哈哈 – linstantnoodles

0

你應該能夠做到與jQuery如下:

$('h1').html('<a href="#">' + $('h1').text() + '</a>'); 

或多個頭

$('h1').each(function() { 
    $(this).html('<a href="#">' + $(this).text() + '</a>'); 
}); 
-1
var word = 'Conference'; 

function highlight(word){ 
    var $container = $('.header-intro-text'); 
    $container.html(
     $container.html().replace(word, '<a href="#">'+word+'</a>') 
    ); 
} 
+0

你一定要緩存你對jquery對象的引用 - 你正在重複選擇! – linstantnoodles

+0

我的壞...編輯 –