簡體   English   中英

如何使用jQuery用文本替換div的內部HTML?

[英]How to replace the inner HTML of a div with text using jquery?

在HTML下方,在頁面中呈現。

<div class="breadcrumb">
<span class="breadcrumb-trail" id="ctl00_ContentPlaceHolder1_ctl00_bc_content"><a title="" href="/default.aspx" target="_self">Home</a> &gt; <a title="" href="/summary.aspx?sid=105&amp;pid=133" target="_self">Summary</a></span>
</div>

關於文檔准備功能,我必須更換

<a title="" href="/summary.aspx?sid=105&amp;pid=133" target="_self">Summary</a>

Summary

這樣,HTML更改為以下內容:

<div class="breadcrumb">
<span class="breadcrumb-trail" id="ctl00_ContentPlaceHolder1_ctl00_bc_content"><a title="" href="/default.aspx" target="_self">Home</a> &gt; Summary</span>
</div>

我該如何使用jQuery呢?

嘗試這個:

$(document).ready(function(){
    var text = $('#ctl00_ContentPlaceHolder1_ctl00_bc_content a:last-child').text();
    $('#ctl00_ContentPlaceHolder1_ctl00_bc_content a:last-child').remove();
    $('#ctl00_ContentPlaceHolder1_ctl00_bc_content').append(text);
});

演示

或者您可以使用:

 var text = $('#ctl00_ContentPlaceHolder1_ctl00_bc_content a:last-child').text();
 $('#ctl00_ContentPlaceHolder1_ctl00_bc_content a:last-child').replaceWith(text);

演示

工作演示 http://jsfiddle.net/B4CrS/

您的ID看起來像是由Visual Studio生成的用戶控件,您可以輕松地做到這一點!

這將滿足您的需求:)

$(document).ready(function () {
    $('a').filter(function (index) {
       if ($(this).text() === "Summary")
           $(this).replaceWith('Summary');
        return $(this).text() === "Summary";
    });

});

使用jQuery.replaceWith

var node = $(".breadcrumb-trail > a:last-child");
var text = node.text();
node.replaceWith( text );

您可以根據需要更改選擇器。 當前選擇器選擇具有面包屑尾隨類的元素中的最后一個子元素“ a”。

$('.breadcrumb-trail a:contains("Summary")').replaceWith('Summary');

要么

$(".breadcrumb-trail a").last().replaceWith('Summary');

可以工作,如果您想縮短它的時間。

開了個小提琴: http : //jsfiddle.net/filever10/erpEN/

考慮使用thttp://api.jquery.com/html/

$(document).ready(function(){
    $("#ctl00_ContentPlaceHolder1_ctl00_bc_content").html("Summary")
}

暫無
暫無

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

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