如何使用 jQuery 将 HTML 属性添加到当前 URL

How to add an HTML attribute to the current URL with jQuery

抱歉,菜鸟 jQuery 问题。这是我的:

HTML:

<div>
    <a class="tag-buttons" href="#" data-id="&product_tag=273" data-element_type="widget" data-widget_type="button.default">
        Sistemas de asistencia
    </a>
</div>

jQuery:

 $(document).ready(function(){
    url = window.location.href + attr('data-id');
      $(".tag-buttons").click(function(){
        $(".tag-buttons").attr("href", url);
      });
    });

我做错了什么?

非常感谢!

这是解决方案

 $(document).ready(function(){
     var url = window.location.href;
       $(".tag-buttons").click(function(){
         url+=$(this).data('id');
         // $(this).attr("href", url);
          window.location = url;
       });
     });

当您设置 url 时,您忘记告诉在哪里获取 data-id 属性。

只需替换:

attr('data-id')

来自

$(".tag-buttons").attr('data-id')