[英]Add an attribute to html element with javascript on page load?
我嘗試過的:
function addAttribute(){
document.getElementById('myid')...
};
window.onload = addAttribute;
如何添加屬性到id="myid"
元素?
document.getElementById('telheaderid').yourattribute = "your_value";
例如
document.getElementById('telheaderid').value = "your_value";
使用jQuery:
$('#telheaderid').attr('value', 'your_value');
編輯:
聚焦是當元素聚焦時觸發的事件,或者例如當我們點擊它突出顯示時間的textarea時。
使用jQuery:
$('#telheaderid').focus(function() {
$(this).val('');
// run any code when the textarea get focused
});
使用普通的javascript:
document.getElementById('telheaderid').addEventListener('focus', function() {
this.value = "";
});
用這個:
document.getElementById('telheaderid').setAttribute('class','YourAttribute')
W3C標准方式:
function functionAddAttribute(){
document.getElementById('telheaderid').setAttribute('attributeName', 'attributeValue');
};
window.onload = functionAddAttribute;
對於IE:
function functionAddAttribute(){
document.getElementById('telheaderid').attributeName = 'attributeValue';
};
window.onload = functionAddAttribute;
享受你的代碼!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.