jQuery将元素ID传递到jquery语句中? [英] jQuery passing element ID into jquery statement?

查看:122
本文介绍了jQuery将元素ID传递到jquery语句中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将元素的ID传递给一个函数,然后该函数调用jQuery.但是,我对如何实际获取ID变量并将其与jQuery语句中的其他文本连接起来感到困惑.例如,这将返回错误:

I'd like to pass the ID of an element into a function which then calls jQuery. However, I'm stumped as to how to actually take the ID variable and concatenate it with other text inside the jQuery statement. For example, this returns an error:

myFunction("#myObject");

function myFunction(IDofObject){

    $("'"+IDofObject+" img'").doSomething...

}

我想对'#myObject img'进行处理,但是无法在语句中使用.

I'd like to do something with '#myObject img' but can't get that to work inside the statement.

推荐答案

不要将参数用引号引起来:

Don't wrap the parameter in quotes:

function myFunction(IDofObject) {
    $( IDofObject + " img" ).doSomething();
}

此外,您可能需要考虑在函数内部添加哈希字符,以便可以向其传递实际的ID,而不是选择器.

Also, you may want to consider adding the hash character inside the function so you can pass it the actual id, not a selector.

myFunction( $('.classSelector :first').attr('id') );

function myFunction(IDofObject) {
    $( "#" + IDofObject + " img" ).doSomething();
}

这篇关于jQuery将元素ID传递到jquery语句中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆