在页面中,用js给textarea 赋值,
以下面的textarea为例子: <textarea name="res" id="res" ></textarea>
需变成 <textarea name="res" id="res" >有值了</textarea>
以下面的textarea为例子: <textarea name="res" id="res" ></textarea>
需变成 <textarea name="res" id="res" >有值了</textarea>
<script language="javascript">
res.value = "有值了";
</script>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>arr_text</title>
<script language="javascript">
function add()
{
document.getElementById('res').value ="有值了";
document.getElementById("res").innerHTML = "有值了";
}
add();
</script>
</head>
<body>
<textarea name="res" id="res" ></textarea>
</body>
多谢一楼 二楼的,可二位的代码都运行不了呀。。
html都没有载入,你让js往哪里加数据?
你的js函数至少要在你要的那个东西载入了以后才执行吧。。
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>arr_text</title>
</head>
<body>
<textarea name="res" id="res" ></textarea>
</body>
<script language="javascript">
function add()
{
document.getElementById('res').value ="有值了";
document.getElementById("res").innerHTML = "有值了";
}
add();
</script>
document.getElementById("res").innerHTML = "有值了";干嘛要写2句?只要其中一句就行了
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>arr_text</title>
<script language="javascript">
window.onload = function()
{
document.getElementById("res").innerHTML = "arr_text";
}
add();
</script>
</head>
<body>
<textarea name="res" id="res" ></textarea>
</body>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>arr_text</title>
<script language="javascript">
window.onload = function()
{
document.getElementById("res").innerHTML = "arr_text";
}
</script>
</head>
<body>
<textarea name="res" id="res" ></textarea>
</body>
给各种控件bind侦听器,根据event来触发业务逻辑。
用jquery时,你会看到头部各种
$(document).ready(function(){
...
$('#elementid').click(function(){
...
});$('#elementid').change(function(){
...
});
});所以建议你养成这种习惯。