开发者

Passing html context to jquery's 2nd param give me null values, why?

I am not sure 开发者_如何学Gowhat I'm doing wrong here:

var typeSelect = "<input class='typeSelect' type='text' value='test' />";
alert($('input', typeSelect).attr('value'));


When you pass a second argument as a context to $(), it's doing this:

$(typeSelect).find('input').attr('value')

...and since that <input> isn't a child, it finds no elements. If the <input> was a child, it would work.

In your case since it's not, you just need this:

$(typeSelect).attr('value')

You can test it here.


alert($(typeSelect).val());

I think!?


The second parameter in JQuery is used for the document scope so it used to pass a document type object and its used for other cases , like if you want to run your JQuery function on another window document or inside an iFrame. in your case you can use this technique :

var typeSelect = document.createElement("div");
$(typeSelect).html("<input class='typeSelect' type='text' value='test' />");

alert($(typeSelect).find("input").attr('value'));

this will create a document object variable with a "div" type and save your html context inside it , and then feel free to do anything on it ;)

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜