在页面加载时使用javascript向html元素添加属性?
我所尝试的:在页面加载时使用javascript向html元素添加属性?,javascript,html,onload,Javascript,Html,Onload,我所尝试的: function addAttribute(){ document.getElementById('myid')... }; window.onload = addAttribute; 如何使用id=“myid”将属性添加到我的元素中 比如说 document.getElementById('telheaderid').value = "your_value"; 使用jQuery: $('#telheaderid').attr('value', 'your_valu
function addAttribute(){
document.getElementById('myid')...
};
window.onload = addAttribute;
如何使用id=“myid”
将属性添加到我的元素中
比如说
document.getElementById('telheaderid').value = "your_value";
使用jQuery:
$('#telheaderid').attr('value', 'your_value');
$('#telheaderid').focus(function() {
$(this).val('');
// run any code when the textarea get focused
});
编辑:焦点是一个事件,当一个元素被聚焦时,或者例如当我们点击它高亮显示的文本区域时,它就会触发 使用jQuery:
$('#telheaderid').attr('value', 'your_value');
$('#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;
享受你的代码 但是我已经可以设置这个类了,我需要添加一个属性,比如“OnClick”,这样我就可以添加另一个函数,OnClick,然后我就可以清除这个值了。这适用于任何属性。你可以用“onclick”、“name”、“style”、“onblur”等替换“class”。谢谢你的帮助,但它不起作用,我真的不知道为什么。您可以尝试这样做:window.onload函数有问题谢谢,您可以尝试用默认值初始化它,然后用javascript清除它吗?下面是链接:您可以使用setAttribute来删除属性为什么不可以?你看到警报框了吗?属性大小是否为空?使用哪种浏览器?哪个版本?尝试在javascript控制台中添加此代码,然后尝试是否有效,那么您一定是在错误的地方运行此代码,或者给我链接或访问权限,如果是管理端,我会在控制台中添加此代码,但如果我将其添加到header.php,则不会,我做错了什么?我可以看到那里的代码..在body标记结束后,在footer.php中添加这个脚本标记,而不是header.php,然后重试!这一次我认为它会起作用。别忘了接受答案!第二种方法是将代码包装在
$(document).ready(function(){/*此处的代码*/})中代码>