简体   繁体   English

如何在动态生成的链接的onClick事件函数中传递JavaScript变量?

[英]How to pass JavaScript variable in a function of onClick event of a dynamically generated link?

I need to pass a JavaScript Variable in a function called redirect(),which will be invoked when a user clicks the link and that link is generated dynamically. 我需要在一个名为redirect()的函数中传递一个JavaScript变量,当用户单击该链接并且该链接是动态生成时,将调用该变量。 here is my code; 这是我的代码;

$(document).ready(function (){

        //this code will generate div when the page loads first time
        var n = 9;
for(var i=0;i<n;i++){
    var div = document.createElement('div');

    div.className = "d5";

    div.id=i+1;
  // alert(div.id);

    document.getElementById('container').appendChild(div);

    $.ajax({ 
    async: false,                                      
      url: 'myapi.php',                  //the script to call to get data          
      data: "",                        //you can insert url argumnets here to pass to api.php for example "id=5&parent=6"
      dataType: 'json',                //data format      
      success: function(data)          //on recieve of reply
      {
        var Name = data[2];
        var subdomain=data[15];
        var uniqueid=data[1];
        var shop_photo=data[3];
        var offer=data[19];              //get id
        //var vname = data[1];           //get name



       //$('#'+div.id).html("<a href='http://www."+subdomain+".xyz.com'>"+Name+"</a>"); 
       //$('#'+div.id).html("<img class='shopperspic' src='b2b/shop_image/"+uniqueid+"/"+shop_photo+"' alt='' /><a href='http://www."+subdomain+".xyz.com'>"+Name+"</a><br>Special Offer<br><p>"+offer+"</p>");
       if(offer=="")
       {
           $('#'+div.id).html("<div class='div1'><img class='shopperspic' src='b2b/shop_image/"+uniqueid+"/"+shop_photo+"' alt='' /></div><div class='div2'><a href='http://www."+subdomain+".xyz.com' onclick='redirect()'>"+Name+"</a></div></div>");
       }
       else
       {
           $('#'+div.id).html("<div class='div1'><img class='shopperspic' src='b2b/shop_image/"+uniqueid+"/"+shop_photo+"' alt='' /></div><div class='div2'><a href='http://www."+subdomain+".xyz.com' onclick='redirect()>"+Name+"</a></div><div class='div3'>Special Offer<br class='br_special'>"+offer+"</div></div>");
       }





      } 



    });




}//this code will generate div when the page loads first time

i need to pass variable var subdomain in the function redirect() 我需要在函数redirect()传递变量var subdomain

and my function redirect() is this: 我的函数redirect()是这样的:

<script type="text/javascript">
    function redirect(a)
    {
        alert(a);//just for showing the passed variable I am using this alert();
    }
    </script>

just insert it to function call with correct type 只需将其插入具有正确类型的函数调用
for string type: 对于字符串类型:

var html = "<a href='http://www."+subdomain+".xyz.com' onclick='redirect(\""+subdomain +"\")>"+Name+"</a>"

Add an id to your anchor tag, for example <a id='id001' href='....' then do document.getElementById("id001"). 将一个ID添加到锚标记中,例如<a id='id001' href='....'然后执行document.getElementById(“ id001”)。 addEventListener("click", function() { redirect(subdomain);}); addEventListener(“ click”,function(){redirect(subdomain);});

尝试这个:

       $('#'+div.id).html("<div class='div1'><img class='shopperspic' src='b2b/shop_image/"+uniqueid+"/"+shop_photo+"' alt='' /></div><div class='div2'><a href='http://www."+subdomain+".xyz.com' onclick='redirect("+ subdomain +")'>"+Name+"</a></div></div>");

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

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