Changing onclick attribute using replace with jQuery

前端 未结 3 1850
终归单人心
终归单人心 2020-12-18 11:15

Do someone know what is the best way to replace some string inside a onclick attribute ?
I need to get the current value and replace some text inside pa

3条回答
  •  梦毁少年i
    2020-12-18 12:04

    $('a[onclick]').attr('onclick', function(i, v){
       return v.replace(/1/g, '2');
    });
    

    http://jsfiddle.net/cj9j7/

    If you need something more dynamic do not use onclick attributes, changing onclick attributes is hackish, you can use click method instead.

    var param = 1;
    $('a').click(function(){
       // ...
    
       if ('wildguess') {
         param = 1;
       } else {
         param++;
       }
    })
    

提交回复
热议问题