将变量传递给 Jquery 函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9222020/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Passing a Variable To A Jquery Function
提问by user988129
I have a list of items that I'd like to perform the same action on. They all have separate IDs so I want to be able to pass the name of each one to Jquery so that the action is only performed on that ID. For example:
我有一个我想对其执行相同操作的项目列表。它们都有单独的 ID,因此我希望能够将每个 ID 的名称传递给 Jquery,以便仅在该 ID 上执行操作。例如:
<a href="#" id="1">one</a>
<a href="#" id="2">two</a>
<div id="#test1"></div>
<div id="#test2"></div>
I want to be able to do something like this:
我希望能够做这样的事情:
function doSomething(x) {
var div=x+'div';
$(x).click(function() { $.(div).slideDown(); });
}
Any thoughts?
有什么想法吗?
Thanks.
谢谢。
回答by Diego
Use the onclick
attribute, that simplifies the code a bit:
使用onclick
属性,可以稍微简化代码:
<a href="#" id="1" onclick="handleClick(this)">one</a>
<a href="#" id="2" onclick="handleClick(this)">two</a>
<div id="#test1"></div>
<div id="#test2"></div>
function handleClick(x) {
$('#test'+ x.id).slideDown();
}
回答by neel4soft
This is how to pass variable in jquery fn
这是如何在 jquery fn 中传递变量
<title>Test</title>
<script type="text/javascript">
$(document).ready(function() {
$("#but").click(function() {
var n = $(this).attr("n");
alert(n);
});
});
</script>
</head>
<body>
<a id="but" href="#" n="I Love You">Click Here</a>
</body>
回答by elclanrs
You can pass jQuery element/s directly into the function like so:
您可以将 jQuery element/s 直接传递到函数中,如下所示:
var doSomething = function(el){
el.click(function(){ });
};
// Single element
var $el = $('a.class');
doSomething($el);
// Multiple elements
var $elms = $('a.class, a#id');
doSomething($elms);
回答by Gigi
<a href="#" class="x" d="test1">one</a> <a href="#" class="x" d="test2">two</a> <div id="test1" class="x"></div> <div id="test2" class="x"></div>
<a href="#" class="x" d="test1">one</a> <a href="#" class="x" d="test2">two</a> <div id="test1" class="x"></div> <div id="test2" class="x"></div>
function doSomething(x)
{
$("a.x").click(function()
{ $( "div#" + $(this).attr("d") ).slideDown(); }
);
}
回答by ShankarSangoli
You can use data
attributes like this to store the data along with html elements and retrieve them using jQuery data()
method. Try this.
您可以使用这样的data
属性将数据与 html 元素一起存储,并使用 jQuerydata()
方法检索它们。尝试这个。
Html change
Html 更改
<a href="#" data-id="1">one</a>
<a href="#" data-id="2">two</a>
<div id="test1"></div>
<div id="test2"></div>
Use this js
使用这个js
$('a').click(function(){
$('#test' + $(this).data('id')).slideDown();
});
To retrieve the data attribute value using jQuery data
method just pass the second half of the attribute. So in this case passing id
to data
will give us the id value.
要使用 jQuerydata
方法检索数据属性值,只需传递属性的后半部分。因此,在这种情况下传球id
给data
会给我们id的值。