Onclick Change Div Title

时间:2013-09-25 18:13:00

标签: javascript html

我喜欢使用onclick函数更改div的标题。我是.js的新手

function changeTitle() {
    document.getElementById("amul").attr('title', 'Item Added');

}

这是我插入了我的onclick功能的输入按钮

<input type="button" class="button2" id="item1" value="Add to Cart" title="Add to Cart" onClick="addItem_check('item_listing_100','ItemTable','100','Amul Butter','500','g','150.00','1','kg','200.00','2','kg','250.00'); amul.style.backgroundColor='#c2ed5c'; if(this.value=='Add to Cart') {this.value = 'Remove from Cart'}; item1();  "/>

这是我想改变标题的div

<div class="center_prod_box" id="amul">. . . </div>

3 个答案:

答案 0 :(得分:4)

怎么样:

document.getElementById("amul").title = "Item Added";

或者

document.getElementById("amul").setAttribute("title", "Item Added");

答案 1 :(得分:3)

您将纯JavaScript(getElementById)与jQuery(attr)混合使用,这两种方法不兼容。尝试其中之一:

// Plain JS (recommended)
document.getElementById("amul").title = "Item Added");

// Plain JS, may not work in <= IE7
document.getElementById("amul").setAttribute("title", "Item Added")

// jQuery (recommended)
$('#amul').attr('title', 'Item Added');

// You can also get the native JS object from a jQuery object
$('#amul')[0].title = "Item Added");
$('#amul')[0].setAttribute("title", "Item Added")

答案 2 :(得分:1)

试试这个: -

document.getElementById("amul").setAttribute("title", "Item Added");