您的位置:首页 > Web前端 > JavaScript

js的HTML属性操作

2016-06-19 22:53 281 查看
<input type="button" id="btn1" value="按钮" />


HTML属性操作:读、写

  属性名

  属性值

属性都操作:获取、找到

  元素.属性

<head>
<meta charset="UTF-8">
<title>Title</title>
<script>
window.onload=function () {
var oBtn=document.getElementById('btn1');
alert(oBtn.value);
};
</script>
</head>
<body>
<input type="button" id="btn1" value="按钮" />
</body>


  如果你不想一访问页面就弹出窗口,那就要加一个点击事件;(匿名函数)

<script>
window.onload=function () {
var oBtn=document.getElementById('btn1');
oBtn.onclick=function () {
alert(oBtn.value);    //调用一个点击事件
};
};
</script>


  再来看一个操作:

<head>
<meta charset="UTF-8">
<title>Title</title>
<script>
window.onload=function () {
var oBtn=document.getElementById('btn1');
var oText=document.getElementById('text1');
oBtn.onclick=function () {
//alert(oBtn.value);
alert(oText.value);
};
};
</script>
</head>
<body>
<input type="text" id="text1" />
<input type="button" id="btn1" value="按钮" />
</body>




<head>
<meta charset="UTF-8">
<title>Title</title>
<script>
window.onload=function () {
var oBtn=document.getElementById('btn1');
var oText=document.getElementById('text1');
var oSelect=document.getElementById('select1');
oBtn.onclick=function () {
//alert(oBtn.value);
//alert(oText.value);
alert(oSelect.value)
};
};
</script>
</head>
<body>
<input type="text" id="text1" />
<select id="select1">
<option value="北京">北京</option>
<option value="上海">上海</option>
<option value="杭州">杭州</option>
</select>
<input type="button" id="btn1" value="按钮" />
</body>


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: