开发者

jQuery: name of jquery object as string?

i wrote a function that takes a string as input.

function ajaxLoad(targetBox, loadUrl) {
   $("div." + targetBox).css("opacity",0.25);
}

so normally i call this function like this. ajaxLoad("commentListBox", "some url");

However right now, i have the problem that I need to pass a object i retrieve via a parent selector. so this is not a string but an object.

var box = $(this开发者_运维问答).parent().parent().toString();
ajaxLoad(box, "some url"); // is a syntax error

How can I handle this?

if i console.log(box);it says [OBJECT]


If you're using class, try:

var box = $(this).parent().parent()[0].className;
ajaxLoad(box, "some url");

If Id, try:

function ajaxLoad(targetBox, loadUrl) {
   $("div#" + targetBox).css("opacity",0.25);
}

var box = $(this).parent().parent()[0].id;
ajaxLoad(box, "some url");


javascript variables are not typed, so it will work. However, if you want to change the type of the parameters, then you are liable to need a separate function. It depends what property of box you actually need.


Could you modify your function somewhat to handle the passing of an object to it?

So, if you check what type the targetBox is and handle it appropriately:

function ajaxLoad(targetBox, loadUrl) {
   var el = "";

   if(typeof(targetBox) === "object") el = targetBox;
   else el = "div." + targetBox;

   $(el).css("opacity",0.25);
}

and then when you call the function, simply pass in the element:

var box = $(this).parent().parent();
ajaxLoad(box, "some url");

Bonus, use a shorthand if for smaller code:

function ajaxLoad(targetBox, loadUrl) {
   var el = typeof(targetBox) === "object" ? targetBox : "div." + targetBox;
   $(el).css("opacity",0.25);
}


You could check the type of 'targetBox' and handle it differently if it's an object:

function ajaxLoad(targetBox, loadUrl) {
    if(typeof(targetBox) == "string")
       $("div." + targetBox).css("opacity",0.25);
    if(typeof(targetBox) == "object")
       $(targetBox).first().css("opacity",0.25);
}

Note: the '.first()' acts as a safety precaution in case your object references multiple elements.


As you are using a class name in the selector, you would get the class name from element:

var box = $(this).parent().parent().attr('className');

However, it's really a detour to get the class name from the element just to find the element again. You can make the function work with both a class name and a jQuery object:

function ajaxLoad(targetBox, loadUrl) {
  if (typeof targetBox !== 'object') targetBox = $("div." + targetBox);
  targetBox.css("opacity", "0.25");
}

Usage:

ajaxLoad($(this).parent().parent(), "some url");

If you only use the function for this, you can just skip the first line in the functon to make it only accept a jQUery object.

Note: You should use the string "0.25" rather than the number 0.25 when you set the opacity style. The number will be converted to a string, and with some locales it will be converted to "0,25" instead of "0.25".

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜