[英]Change a <div id=“”> to <div data-layout=“”> using Jquery?
So I am building a tumblr template, and I am using stylehatch's Photoset-grid plugin. 因此,我正在构建一个tumblr模板,并且正在使用stylehatch的Photoset-grid插件。 The plugin works, and in the custom html section I can make a fully functional grid. 该插件有效,并且在自定义html部分中,我可以创建一个功能齐全的网格。
The problem is, whenever I make a new tumblr post and edit the posts html to include the "data-layout" portion of the div, tumblr erases it. 问题是,每当我创建一个新的tumblr帖子并编辑html帖子以包含div的“数据布局”部分时,tumblr都会删除它。 (Tumblr posts can only contain custom classes and id's in div's, apparently) (显然,Tumblr帖子只能包含div中的自定义类和ID)
The markup I need is this: 我需要的标记是这样的:
<div class="photoset-grid" data-layout="132">
But tumblr erases the data-layout part when I save the post, so i get this: 但是当我保存帖子时,tumblr删除了数据布局部分,所以我得到了:
<div class="photoset-grid">
My question is this: Can I use jquery to substitute a div id for a data layout? 我的问题是:我可以使用jquery代替div ID进行数据布局吗?
So If I write this: 所以,如果我这样写:
<div class="photoset-grid" id="132">
Can jquery change it to this, before the plugin loads so it works correctly? 插件加载之前,jQuery是否可以将其更改为此,以使其正常工作?
<div class="photoset-grid" data-layout="132">
You can't substitute attributes directly, but you can simply get the id attribute value, create the new data attribute, and remove the ID attribute. 您不能直接替换属性,但是可以简单地获取id属性值,创建新的data属性并删除ID属性。
$(".photoset-grid").attr("data-layout", function () { return this.id })
.removeAttr("id");
You can do it in a few steps. 您可以按照几个步骤进行操作。 First, save off the id value. 首先,保存id值。 Second, create a 'data-layout' attribute with that value. 其次,使用该值创建一个“数据布局”属性。 Lastly, remove the id attribute. 最后,删除id属性。
$('.photoset-grid').each(function(){
var identifier = $(this).attr('id');
$(this).attr('data-layout',identifier).removeAttr('id');
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.