在jquery的帮助下修改元素的id

时间:2012-05-03 18:14:21

标签: php jquery jquery-ui

我想在jquery

的帮助下修改元素id
<img src="images/AOL_button.png" id='http://openid.aol.com/***username***' class="indirect" />
<img src="images/google_button.png" id='https://www.***username***.google.com/accounts/o8/id' class="direct"/> 

所以使用jquery我想根据条件

将“username”的值更改为其他内容
$(.direct).click(function(){
   var username=userA;
   // replace username from id here
})

$(.direct).click(function(){
   var username=userB;
   // replace username from id here
})

1 个答案:

答案 0 :(得分:5)

尝试如下,

$('.direct').click(function(){
   var username=userA;
   //            ^--assuming userA is a var in scope 
   //            if it is a string, just use it below as "userA"(including the quotes)
   this.id = this.id.replace('***username***', username);
});