简体   繁体   English

将参数传递给JavaScript中的函数

[英]Passing a parameter to a function in Javascript

I need to create a Javascript function (using a loop) that receives a value as a parameter from a function call in HTML and in response passes back the image of the back of a playing card however many times the value of the parameter is. 我需要创建一个Javascript函数(使用循环),该函数从HTML中的函数调用中接收一个值作为参数,然后作为响应,将扑克牌背面的图像传回,但是该参数的值是该值的许多倍。

For example, if I pass the value 5 to the function, I should get back 5 images of the back of the card from the function displayed on the HTML page inside an existing table. 例如,如果我将值5传递给该函数,则应该从现有表内HTML页面上显示的函数中获取卡片背面的5张图像。

Here is what I have so far. 这是我到目前为止所拥有的。 Can someone please point me in the right direction as to where I'm going wrong (Thank you in advance for any help). 有人可以向我指出我要去哪里的正确方向(预先感谢您的帮助)。

JS JS

function showCards(numcards) { 
  var data = ""; 
  while (numcards < 5) {
    data += "<td><img src="http://www.college1.com/images/cards/gbCard52.gif" NAME="card0"></td>";
    numcards +=;
  }
  document.writeln(data);
}

HTML 的HTML

<table border=0 style='margin:auto'>
  <tr> 
   <td>
     <form>
       <input type="BUTTON" onClick="Javascript:alert('Dummy Link')" value="Deal > > >">
     </form>
   </td> 
   <script type="text/javascript">showCards(5)</script>   
   <td>
     <form>
       <input type="BUTTON" onClick="Javascript:alert('Dummy Link')" value="< < < Hit Me">
     </form>
   </td>
 </tr>
</table> 

There are mainly syntax errors in your code. 您的代码中主要存在语法错误。

  1. Either escape the double quote inside a string (ie. "\\"" ) or use single quotes to declare your string. 可以在字符串中转义双引号(即"\\"" ),也可以使用单引号声明您的字符串。

  2. You were trying to increment numcards using a wrong syntax. 您试图使用错误的语法来增加numcards The correct syntax is numcards++ . 正确的语法是numcards++

  3. But that's not what you want, you want to decrement it to 0 (ie. numcard-- ) to get your number of cards or count up to numcards using a for loop for example. 但这不是您想要的,您想将其递减为0(即numcard-- )以获取卡数或使用for loop计数到numcards

 function showCards(numcards) { var data = ""; for (var i = 0; i < numcards; i++) { data += '<td><img src="http://www.college1.com/images/cards/gbCard52.gif" NAME="card0"></td>'; } document.writeln(data); } showCards(5); 
 <table border=0 style='margin:auto'> <tr> <td> <form><input type="BUTTON" onClick="Javascript:alert('Dummy Link')" value="Deal > > >"></form> </td> <td> <form><input type="BUTTON" onClick="Javascript:alert('Dummy Link')" value="< < < Hit Me"></form> </td> </tr> </table> 

 // where you want to append images tag var table = document.querySelector('tbody'); // you can call this method on click event function show(elem , param) { var i = 0; var data = document.createElement("tr"); var td = ""; while (param != i) { td += "<td><img src='http://www.college1.com/images/cards/gbCard52.gif' NAME='card"+i+"'></td>"; i++; } data.innerHTML = td; // append html tr node elem.appendChild(data); } // calling when the script is loaded show(table, 5); 
 <table> <tbody> </tbody> </table> 

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

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