简体   繁体   English

使用html()函数更改link href属性

[英]change link href attribute with html() function

If i have a link in my page.I want to change its href attribute after some event is triggered how i can do that 如果我的页面中有链接,我想在触发某些事件后更改其href属性

Button 按键

<a id="btn-start" href="/dashboard/save/{{ pk }}" class="btn btn-primary btn-embossed">Save Reward</a>

Javascript 使用Javascript

$( "#sortable1 div" ).sortable({
        connectWith: "div",
        stop: function( event, ui ) {

            if($('#sortable2').find('img').length==6) {
                $('#btn-start').html("<a id='btn-start' href='/dashboard/redeem/{{ pk }}' class='btn btn-danger' >Redeem Coupon</a>");
            }
        }
    });

html() will change the inner HTML of your element. html()将更改元素的内部HTML As a result, your code adds another <a> element inside your #btn-start element. 结果,您的代码在#btn-start元素内添加了另一个<a>元素。

You can use the prop() function to change the actually HTML property (and attribute): 您可以使用prop()函数更改实际的HTML属性(和属性):

if($('#sortable2').find('img').length == 6) 
{
    $('#btn-start').prop('href', '/dashboard/redeem/{{ pk }}');
}
$('#btn-start').attr ('href', "http://sample");

As per your code you change href,class and innerhtml of "#btn-start".If its below is the working code 根据您的代码,更改“#btn-start”的href,class和innerhtml。如果下面的代码是工作代码

 $('#btn-start').attr('href=','/dashboard/redeem/{{ pk }}')
 .attr('class','btn btn-danger').html('Redeem Coupon');

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

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