onload事件调用带有两个参数的java脚本函数 [英] onload event call java-script function with two parameters

查看:181
本文介绍了onload事件调用带有两个参数的java脚本函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个java脚本函数,它将在我的html页面中动态生成按钮。

我的java脚本是:

< script type =   java / script> 

function myFunc(target_div,num_buttons){
var 个按钮= ;
for var i = 0 ; i< num_buttons; i ++){
// 添加了类------- --------- v
按钮+ = ' <输入类型=buttonid =button _' + i + ' class =mybuttonsvalue =按钮_' + i + ' >< / input>';
}
target_div.html(按钮);
var doButtonPress = function (i){
alert(i); // 我实际上在这里做了一些更复杂的事情,但现在并不重要
}

// 是的,mybuttons存在
$( .mybuttons')。click( function (){
doButtonPress( this .id.replace(' button _'' '));
// this.id.replace('button_','')< - 返回i值
}) ;
}
< / script>



在HTML中是:

 <   body      önload  =  myFunc(this,'10'); >  
< 表格 >
< div id = mydiv > < / div >
< / form >

<? php
// 将代码放在此处
?>
< / body >



我不知道为什么这不会调用myFunc(这个,''10'')。是否有任何问题通过我的代码传递id和数字?

解决方案

' .mybuttons')。click( function (){
doButtonPress( this .id.replace(' button _'' ));
// this.id.replace('button_','')< - 返回i值
});
}
< / script>



在HTML中是:

 <   body      önload  =  myFunc(this,'10'); >  
< 表格 >
< div id = mydiv > < / div >
< / form >

<? php
// 将代码放在此处
?>
< / body >



我不知道为什么这不会调用myFunc(这个,''10'')。有什么问题可以通过我的代码传递id和数字吗?


首先,body标签应该是这样的。





 <   body      önload  =   myFunc(this,'10'); >  

这应该是这样的

< body onload = myFunc(this,'10'); >

你写önload它应该是onload


添加这个

 

I have a java-script function which will generate buttons dynamically in my html page.
My java-script is:

<script type="java/script">
            
            function myFunc(target_div,num_buttons) {
                var buttons = "";
                for (var i = 0; i < num_buttons; i++) {
                    //                             added class ----------------v
                    buttons += '<input type="button" id="button_' + i + '" class="mybuttons" value="button_' + i + '"></input>';
                }
                target_div.html(buttons);
                var doButtonPress = function(i) {
                    alert(i); // I actually do something more complicated here, but it's not important now
                }

                //yes, mybuttons exist 
                $('.mybuttons').click(function() {
                    doButtonPress(this.id.replace('button_', ''));
                    //this.id.replace('button_', '') <- returns i value
                });
            }
        </script>


And in the HTML is:

<body  önload="myFunc(this,'10');">
        <form>
            <div id="mydiv" ></div>
        </form>
        
        <?php
        // put your code here
        ?>
    </body>


I don''t know why this not calling the myFunc(this, ''10''). Is the any problem to pass id and number by my code ?

解决方案

('.mybuttons').click(function() { doButtonPress(this.id.replace('button_', '')); //this.id.replace('button_', '') <- returns i value }); } </script>


And in the HTML is:

<body  önload="myFunc(this,'10');">
        <form>
            <div id="mydiv" ></div>
        </form>
        
        <?php
        // put your code here
        ?>
    </body>


I don''t know why this not calling the myFunc(this, ''10''). Is the any problem to pass id and number by my code ?


First the body tag should be like this.


<body  önload="myFunc(this,'10');">

this should be like this

<body  onload="myFunc(this,'10');">

you write "önload" it should be "onload"


add this


这篇关于onload事件调用带有两个参数的java脚本函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆