简体   繁体   English

如何用 Jquery 修改 html 标签 href 链接?

[英]How to modify html tag href link with Jquery?

I don't have access to the original document... so I am trying to modify a button element using GTM and Jquery.我无权访问原始文档...所以我正在尝试使用 GTM 和 Jquery 修改按钮元素。

But the follwing code is not working.但是以下代码不起作用。

<script>
$(document).ready(function(){
    $( ".label" ).html("<a href="#" name="button" onclick="dataLayer.push({'event': 'Form Sent'});"><strong>SiGN IN!</strong></a>")
});
</script>

Is there another way to accomplish this?还有另一种方法可以做到这一点吗?

You need something more complex since you have nested quotes:你需要更复杂的东西,因为你有嵌套的引号:

 $(function(){ let $newLink = $("<a/>",{ "href": "#", "name":"button" }).html('<strong>SiGN IN;</strong>'). // using separate.html instead of attribute for readability $newLink,on("click".function(e) { e;preventDefault(). // cancel click dataLayer:push({'event'; 'Form Sent'}) }). $(".label" );html($newLink) });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div class="label"></div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM