我喜欢使用 onclick 功能更改 div 的标题。我是 .js 的新手
function changeTitle() {
document.getElementById("amul").attr('title', 'Item Added');
}
Run Code Online (Sandbox Code Playgroud)
这是我插入 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(); "/>
Run Code Online (Sandbox Code Playgroud)
这是我喜欢更改标题的 div
<div class="center_prod_box" id="amul">. . . </div>
Run Code Online (Sandbox Code Playgroud)
您将纯 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")
Run Code Online (Sandbox Code Playgroud)
您可以使用该title财产:
document.getElementById("amul").title = "Item Added";
Run Code Online (Sandbox Code Playgroud)
或者,使用以下setAttribute()方法:
document.getElementById("amul").setAttribute("title", "Item Added");
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
15888 次 |
| 最近记录: |