开发者

replacing all instances on a page - javascript

开发者 https://www.devze.com 2022-12-20 02:19 出处:网络
Hi I would like to replace all href and action attributes on a page with href=\"#\" and action=\"#\".Could someone point out where i am going wrong;

Hi I would like to replace all href and action attributes on a page with href="#" and action="#". Could someone point out where i am going wrong;

$(document).ready(function开发者_如何学编程(){
var contents = $("body").html();
contents.replace( /href=[\"'][^'\"]*[\"']/g, 'href="#"' );
contents.replace( /action=[\"'][^'\"]*[\"']/g, 'action="#"' );
});

I would also like to do this without the use of jQuery but not sure how.


Why don't you try something like :

$('a').attr('href','#');
$('form').attr('action','#');


$(document).ready(function(){
    $("a, area, form").each(function(){
        if (typeof $(this).attr("href") != 'undefined') {
            $(this).attr("href", "#");
        }else if(typeof $(this).attr("action") != 'undefined') {
            $(this).attr("action", "#");
        }
    });
});

I added a tick to Soufiane Hassou answer as I found the answer via his comment, thanks

0

精彩评论

暂无评论...
验证码 换一张
取 消