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
精彩评论