简体   繁体   中英

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.

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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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