简体   繁体   中英

Object + class names as variable (jQuery)

I got an error, if I try this:

var Box = $(window.parent.document).find("#box"); // works fine
var BoxContent = $(Box+" .bg > .content").text(); // error

console.log(BoxContent);

Error message ("Uncaught Error: Syntax error, unrecognized expression: [object Object] .bg .content")

What is my fail?

Box isn't a string, you can't meaningfully concatenate it with a string to create a new selector.

It's a jQuery object, so you can use .find() to search within it.

var BoxContent = Box.find(".bg > .content").text();

try this one

var BoxContent = Box.find(".bg > .content").first().text();
// OR
var BoxContent = Box.find(".bg").first().find(".content").first().text();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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