获取属性的值并将其粘贴到 jQuery 中

问题描述 投票:0回答:1

所以,我有 2 个按钮,每个按钮都有一个属性和一个值。现在在我的 jQuery 代码中,我试图粘贴点击了哪个按钮的属性值。

所以这里的问题是,当我单击其中一个按钮时,它不会从该属性更新 cookie 中的值。单击其中一个按钮时,我还会得到其他值作为值。我在下面留下了截图。 screenshot

$(document)['ready'](function() {
  $("[accent-switcher]").each(function(accentSwitcher) {
    $(this)['click'](function() {
      ips['utils']['cookie']['unset']('ipsTheme_type');
      ips['utils']['cookie']['set']('ipsTheme_type', $(this).val($(this).attr("accent-switcher")), true)
    });
  });
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>


<button class='ipsButton' accent-switcher="blue">Color A</button>
<button class='ipsButton' accent-switcher="yellow">Color B</button>

html jquery
1个回答
0
投票

试试这个,这应该有效,或者尝试“运行代码片段”按钮自己测试一下

$( document ).ready(function() {
    $( "html body .ipsButton" ).on( "click", function() {
        var accentSwitcher = $(this).data('accent-switcher');
        alert('accent-switcher:' + accentSwitcher);
    });
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<html>
    <body>
        <button class="ipsButton" data-accent-switcher="blue">Color A</button>
        <button class="ipsButton" data-accent-switcher="yellow">Color B</button>
    </body>
</html>

© www.soinside.com 2019 - 2024. All rights reserved.