javascript Onclick 更改 Div 标题
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19012218/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Onclick Change Div Title
提问by Zain
I like to change Title of a div using onclick function. I a new to .js
我喜欢使用 onclick 功能更改 div 的标题。我是 .js 的新手
function changeTitle() {
document.getElementById("amul").attr('title', 'Item Added');
}
This is the input button where i have inserted my onclick function
这是我插入 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(); "/>
This is the div which i like to change the title
这是我喜欢更改标题的 div
<div class="center_prod_box" id="amul">. . . </div>
回答by tymeJV
How about:
怎么样:
document.getElementById("amul").title = "Item Added";
Or
或者
document.getElementById("amul").setAttribute("title", "Item Added");
回答by Daniel Imms
You're mixing plain JavaScript (getElementById
) with jQuery (attr
), the two methods are incompatible. Try one of these:
您将纯 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")
回答by Rahul Tripathi
Try this:-
试试这个:-
document.getElementById("amul").setAttribute("title", "Item Added");