簡體   English   中英

在文本周圍包裝HTML

[英]Wrap HTML around text

這不是理想的,但是我試圖在我的標題周圍包裝一些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>

嘗試:

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

您應該能夠使用jQuery執行以下操作:

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

或多個標題

$('h1').each(function() {
    $(this).html('<a href="#">' + $(this).text() + '</a>');
});
var hit = $('.header-intro-text h1');
var hitText = hit.text();
hit.html($('<a href="#"></a>').text(hitText));

演示: http//jsfiddle.net/qwertynl/reV9V/

var word = 'Conference';

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM