❶ js动态添加的DIV中的onclick事件简单实例
最简单的是这样:<input type="button" onclick="alert(this.value)" value="我是 button" />动态添加onclick事件:
<input type="button" value="我是 button" id="bu"><script type="text/javascript">var bObj=document.getElementById("bu");bObj.onclick= objclick;function objclick(){alert(this.value)};</script>
如果使用匿名函数 function(){},则如下面所示:
<input type="button" value="我是 button" id="bu"><script type="text/javascript">var bObj=document.getElementById("bu");bObj.onclick=function(){alert(this.value)};</script>
上面的方法其实原理都一样,都是定义 onclick 属性的值。值得注意的是,如果多次定义 obj.onclick,例如:obj.onclick=method1; obj.onclick=method2; obj.onclick=method3,那么只有最后一次的定义obj.onclick=method3才生效,前两次的定义都给最后一次的覆盖掉了。
再看 IE 中的 attachEvent:
<input type="button" value="我是拉登" id="bu"><script type="text/javascript">var bObj = document.getElementById("bu");bObj.attachEvent("onclick"method1);bObj.attachEvent("onclick"method2);bObj.attachEvent("onclick"method3);function method1(){alert("第一个alert")}function method2(){alert("第二个alert")}function method3(){alert("第三个alert")}</script>
执行顺序是 method3 > method2 > method1 ,先进后出,与堆栈中的变量相似。需要注意的是attachEvent 中第一个参数是on开头的,可以是 onclick/onmouseover/onfocus 等等
据说(未经确认验证)在 IE 中使用 attachEvent 后最好再使用 detachEvent 来释放内存
再看看 Firefox 中的的 addEventListener:
<input type="button" value="我是布什" id="bu"><script type="text/javascript">var bObj = document.getElementById("bu");bObj.addEventListener("click"method1,false);bObj.addEventListener("click"method2,false);bObj.addEventListener("click"method3,false);function method1(){alert("第一个alert")}function method2(){alert("第二个alert")}function method3(){alert("第三个alert")}</script>
可以看到,在 ff 中的执行顺序是 method1 > method2 > method3 , 刚好与 IE 相反,先进先出。需要注意的是 addEventListener 有三个参数,第一个是不带“on”的事件名称,如 click/mouseover/focus等。
❷ JS如何控制button的位置
解决方法:
1、把button定义成绝对定位,position:absoulte的方式,然后设置left,top的方式进行位置控制
2、如果是节点移动,则可以通过dom删除和增加的方式来调整位置
问题解决:
这里针对的是第二种情况,可以把对应的节点获取后,删除再插入到对应的节点后。
代码示例:
<script>
functionmove(self){
varp=self.parentNode;//获取当前节点的父节点
self.remove();//移除当前节点
p.appendChild(self);//父节点添加当前节点
}
</script>
</head>
<body>
<div>
<inputtype="button"id="button1"value="1"onclick="move(this)">
<inputtype="button"id="button2"value="2"/>
</div>
</body>
❸ button按钮的属性设置
button按钮的属性设置:
1、name:表示按钮的名称,通常作为按钮标识进行使用。
2、type:表示按钮类型,通常与表单一起联用。reset:重置按钮sumit:提交按钮button:普通按钮。
3、value:表示按钮初始值,通常在js脚本中进行使用和修改。
4、disabled:表示禁用按钮,使按钮不能点击END。
5、autoplay:当页面加载时按钮应当自动地获得焦点。在实例中,我们会看到,第一次打开页面的时候,发现按钮出现了蓝色边框,就是所谓的焦点。
button的意思解释如下:
button的中文释义:
1、当词性为名词时,意为纽扣,扣子;按钮;美徽章;不值钱的东西;按钮广告;按钮层。
2、当词性为动词时,意为扣上;把…的纽扣扣上;用纽扣扣住;钉扣子。
3、当用作人名时,可翻译为(Button)(英)巴顿。
button的读法:button的英式发音为[?b?t(?)n];美式发音为[?b?t(?)n]。
短语搭配:
1、hot button引起强烈争论的话题。
2、on the button准时,正好。
3、press the button按电钮启动。
4、radio button单选按钮。
5、belly button(人的)肚脐。
双语例句:
1、I pushed the button for the top floor.
我按了到顶层的按钮。
2、This button is for adjusting the volume.
这个按钮是调节音量的。
3、Anyone can sew on a button,including you.
任何人都能缝钮扣,包括你。
4、Drago pressed a button and the door closed.
德拉戈按了一个按钮,门关上了。
5、She pressed the button but nothing happened.
她按下按钮,但什么反应也没有。
6、Adam pressed a button and waited for the lift.
亚当按了一个按钮,然后等着乘坐电梯。
7、He ripped away a wire that led to the alarm button.
他把连接报警按钮的电线扯掉了。
8、You may have inadvertently pressed the wrong button.
你也许无意中按错了按钮。
9、I sit down,thread a needle,snip off an old button.
我坐下来,穿好针,剪下了一粒旧钮扣。
10、Mrs.Baylor strode to the lift and punched the button.
贝勒太太大步走到电梯前按了一下按钮。
❹ 在JSP中如何获取Button按钮中的Value值
1、创建一来个名称为 type_button 的html文件 。
❺ js给动态创建的按钮添加动态事件
这是源JavaScript经典的闭包问题
你需要
document.getElementById('king'+i).onclick=function(i)
{
returnfunction()
{
document.getElementById('shipin').src=arr[i]
}
}(i)
❻ 在javascript中怎么设置button的可点击和不可点击
、js中设置按钮可点击与不可点击,默认是可点击的
(1)设置按钮不可版点击权
document.getElementById("bt1").disabled=ture;
(2)设置按钮可点击
document.getElementById("bt1").disabled=false;
2、jq中设置按钮可点击与不可点击,默认是可点击的
(1)设置按钮不可点击
$("#bt1").attr("disabled",ture);
(1)设置按钮可点击
$("#bt1").attr("disabled",false);
3、标签中设置按钮不可点击
在标签中添加属性disabled="true"。