简体   繁体   English

如何在jquery中的onclick中传递参数

[英]How to pass parameter in onclick in jquery

The code below is calling In index.html 下面的代码是在index.html中调用

    <script src="js/jquery/jquery-1.8.2.min.js"></script>
    <script src="js/px.js" type="text/javascript" ></script>
    <script type="text/javascript">
        var newGame = new px1("black");
        new px1Generator(newGame);
    </script>

the code below is in px.js 下面的代码在px.js中

var px1 = function (color) {
   if (color == "white") {
        this.x = "100";
   } else if (color == "black") {
        this.x = "900";
   }};
var px1Generator= function (px1obj) {
    for (i = 0; i < 10; i++) {
        $("#mainGame").append("<div class='pawn white'></div>");
    }
    //for 10 object upon add onClickEvent
    $(".pawn.white").on("click", function (event, px1obj) {
       alert(px1obj.x)
    });
};

I want to send 100 or 900 to onclick function, how to do it ? 我想发送100或900到onclick功能,怎么做? The error accord when user clicks on divs 用户单击div时错误一致

You have two issues here. 你有两个问题。 Firstly, when you run your code right now you'll see this error: 首先,当你立即运行代码时,你会看到这个错误:

Uncaught TypeError: px1 is not a constructor 未捕获的TypeError:px1不是构造函数

This is because you're trying to instantiate the function before it exists. 这是因为您尝试在函数存在之前对其进行实例化。 You need to either define your functions first, or change to use explicit function definitions. 您需要先定义函数,或更改为使用显式函数定义。

Secondly, the problem itself it because you've redefined px1obj as a parameter on the click event handler which will never be defined. 其次,问题本身就是因为你已经将px1obj重新定义为click事件处理程序的一个参数,它永远不会被定义。 This hides the px1obj defined in the outser scope. 这会隐藏px1obj在outser范围限定。 To fix this simply remove that parameter from the event handler: 要解决此问题,只需从事件处理程序中删除该参数:

 var newGame = new px1("black"); new px1Generator(newGame); function px1(color) { if (color == "white") { this.x = "100"; } else if (color == "black") { this.x = "900"; } }; function px1Generator(px1obj) { for (i = 0; i < 10; i++) { $("#mainGame").append("<div class='pawn white'></div>"); } //for 10 object upon add onClickEvent $(".pawn.white").on("click", function(e) { console.log(px1obj.x) }); }; 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script> <div class="pawn white">pawn white</div> 

You need to remove the px1obj param from the on click callback function signature, because you are overriding it. 您需要从单击回调函数签名中删除px1obj参数,因为您要覆盖它。

px1obj already exists in the px1Generator function scope, and you are overriding it inside the onclick callback scope. px1obj已存在于px1Generator函数作用域中,并且您将在onclick回调作用域内覆盖它。

 $(function() { function px1(color) { if (color == "white") { this.x = "100"; } else if (color == "black") { this.x = "900"; }}; function px1Generator(px1obj) { for (i = 0; i < 10; i++) { $("#mainGame").append("<div class='pawn white'>a</div>"); } //for 10 object upon add onClickEvent $(".pawn.white").on("click", function (event) { console.log(px1obj.x) }); }; var a = new px1("black"); px1Generator(a); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <div id="mainGame"></div> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM