简体   繁体   English

JQuery中的动态参数

[英]Dynamic parameters in JQuery

I have function in which append method is taking a static parameter. 我有函数,其中append方法采用静态参数。

 function toggle() {
         $("#MS").append($('#hide'));        
    }

What i want is to pass the parameter dynamically from my Hyperlinks click event. 我想要的是从我的超链接click事件动态传递参数。 In above code that #MS is static which i want to pass dynamically 在上面的代码中,#MS是静态的,我想动态传递

Code: HTML 代码:HTML

<div id="MS">
        <a href="javascript:void(0);" onclick="toggle();">JP MORGAN</a><br>
    </div>

I want to pass the argument from onclick to toggle method and that parameter will be used in the append method. 我想将参数从onclick传递到切换方法,该参数将在append方法中使用。

I have tried several combinations buut it didnt worked. 我尝试了几种组合buut,但没有成功。 Please help.. 请帮忙..

My new code after changes 更改后我的新代码

<script>
$(function() { // when the DOM is ready
    var $hide = $('#hide').click(function(){
       $(this).closest('div').hide();
    }); 

    $('a.toggle').click(function(e){
       e.preventDefault();
       $(this).parent().append($hide);
    });
}); 


</script>

<div id="JP">
        <a href="#">JP MORGAN</a><br>       
    </div>

still not working 还是行不通

Since you are using jQuery, you can add classes to your a elements and use parent method: 既然你使用jQuery,您可以添加类的a元素,并使用parent方法:

$(function() { // when the DOM is ready
    var $hide = $('#hide').click(function(){
       $(this).closest('div').hide();
    }); 

    $('a.toggle').click(function(e){
       e.preventDefault();
       $(this).parent().append($hide);
    });
}); 

Retrieve the ID dynamically on the anchor's click event and pass that ID to the function: 在锚点的click事件上动态检索ID ,并将该ID传递给函数:

$("a").on("click", function(e){
    e.preventDefault();

    $(this).append($('#hide')); 

};

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

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