我試圖弄清楚如何撰寫 jQuery 代碼來將元資料插入到元素中,特別是這個:data-section-name="home"
我想將其插入到 ID 為的 div 中,home-section輸出將如下所示:
<div id="home-section" data-section-name="home">
(some code here...)
</div>
我在 Wordpress 中使用 divi 構建器
uj5u.com熱心網友回復:
如果您需要該data屬性出現在 HTML 源代碼中,請使用attr():
$('#home-section').attr('data-section-name', 'home');
如果資料屬性將被 jQuery 庫讀取,那么您可以改用該data()方法,該方法性能更高:
$('#home-section').data('section-name', 'home');
在這兩種情況下的警告是確保您在任何庫依賴于該屬性存在之前執行這行代碼。data
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/442732.html
