Tag: outerhtml

  • Get the markup of an element, including itself using jQuery

    接手同事的 code,需要接續 AJAX 的資料對前端頁面進行更動。 假設一段 html code 如下: <div id=”container” class=”wrapper”> <div id=”one”>test 1 </div> <div id=”two”>test 2 </div> <div id=”three”>test 3 </div> <div id=”four”>test 4 </div> </div> 以下 jQuery 語法可以其得其中的 html 元素: $(‘#container’).html(); 不過卻不包含 <div id=”container”> 本身,如果要取得包含自身的 html,應改為如下: $(‘#container’).get(0).outerHTML; 注意不論是取 $(‘.wrapper’) 還是 $(‘#container’) 都要用 get(0) 指定 DOM 元素。