Code using jQuery in Chrome content script fails to work
I'm working on a Chrome extension 开发者_高级运维that injects content into the tumblr 'new posts' page, which has an element with id tag_editor. But I can't get at it using jQuery:
$(document).ready(function() {
alert($("#tag_editor"));
});
just gives me [object Object], which I assume means that it failed to find an element. If I use document.getElementById instead of $, it works. I have jQuery.js in my manifest.json, so it's not that jQuery isn't loading. What am I doing wrong?
object Object means its returning a jQuery object. You cant alert an object. Try this:
$(document).ready(function() {
console.log( $("#tag_editor") );
});
Or
$(document).ready(function() {
alert($("#tag_editor").html());
});
The jQuery function, $()
, returns an object; hence your alert
message. If you want the underlying DOM object, say:
alert($("#tag_editor")[0]);
or
alert($("#tag_editor").get(0));
精彩评论