gpt4 book ai didi

javascript - 如何在 jQuery 中编辑数据属性值?

转载 作者:行者123 更新时间:2023-12-03 03:48:57 25 4
gpt4 key购买 nike

我正在努力……

将显示的数据中的黄色一词替换为红色......

然后...将单词 margin20 添加到 data-shown

然后...将单词 padding20 添加到 data-shown

然后...从显示的数据中删除“工具”一词

$(function() {
var data = $(".testclass").attr('data-shown');
data = data.replace('yellow', 'red');
$(".testclass").attr('data-shown', data);

alert($(".testclass").attr('data-shown'));
});

//alert should read - w h red margin80 padding20
[data-shown~="w"]	{width: 100px;}
[data-shown~="h"] {height: 100px;}
[data-shown~="yellow"] {background: yellow;}
[data-shown~="red"] {background: red;}
[data-shown~="margin80"] {margin: 80px;}
[data-shown~="padding20"] {padding: 20px;}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div data-shown="w h yellow tools" class="testclass">test</div>

最佳答案

希望是你所需要的

$(function() {
var data = $(".testclass").attr('data-shown');
data = data.replace('yellow', 'red');
$(".testclass").attr('data-shown', data);

alert($(".testclass").attr('data-shown'));
});
[data-shown~="w"]	{width: 100px;}
[data-shown~="h"] {height: 100px;}
[data-shown~="yellow"] {background: yellow;}
[data-shown~="red"] {background: red;}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div data-shown="w h yellow" class="testclass">test</div>

关于javascript - 如何在 jQuery 中编辑数据属性值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45250273/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com