替換標籤名-兩種

<div>換成<p>標籤
html:

<div>
    <div id="tag"></div>
</div>
<button onclick="changeTagNameOne (tag)">切換標籤名</button>
<button onclick="changeTagNameTwo (tag)">切換標籤名</button>

js:

function changeTagNameOne (id) {
    $(document).ready(function () {
        let $div = $("#"+id);
        let html = $div.get(0).outerHtml;
        html = html.replace(/^<div/ , "<p");
        html = html.replace(/^/div>$/ , "/p>");
        $div.replaceWith(html);
    }
}

function changeTagNameTwo (id) {
    let element = $('<p></p>').html($("#tag").html());
    $("#tag").prop('outerHTML',element.prop('outerHTML'));
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章