2
我有一個腳本和jQuery在我的插件和腳本運行到頁面面板。 如何將對象從我的面板複製到頁面?如何在Firefox中的腳本之間移動jQuery對象?
main.js
var tabs = require("sdk/tabs");
var self = require("sdk/self");
var button = require("sdk/ui/button/action").ActionButton({
id: "show-panel",
label: "Show Panel",
icon: {
"16": "./icon-16.png",
"32": "./icon-32.png",
"64": "./icon-64.png"
},
onClick: handleClick
});
var panel = require("sdk/panel").Panel({
contentURL: self.data.url("page.html"),
contentScriptFile: [self.data.url("jquery-2.1.1.min.js"), self.data.url("panel-script.js")],
position: button
});
function handleClick() {
panel.show();
var worker = tabs.activeTab.attach({
contentScriptFile: [self.data.url("jquery-2.1.1.min.js"), self.data.url("page-script.js")]
});
panel.port.emit('to-panel');
panel.port.on("from-panel", function(pic) {
worker.port.emit("to-page", pic);
});
}
panel.html
<html>
<head>
</head>
<body>
<img src="icon-64.png" class="add-img" alt="">
<button class="click">Click Me!</button>
</body>
</html>
panel.js
self.port.on("to-panel", function() {
var pic = $('.add-img');
$('button.click').on('click', function(){
self.port.emit("from-panel", pic);
});
});
page.js
self.port.on("to-page", function(pic) {
$('body').append(pic);
});
我想從我的面板複製元素並將其放置到頁面。 我試着將它設置爲名爲'pic'的變量並傳輸到page.js以放置在網頁上,但它不起作用。儘管如此,簡單的字符串很容易傳輸並放置在頁面上。
瀏覽器控制檯顯示「TypeError:a is undefined」。
請幫忙,對不起英文不好。
它並沒有幫助。我忘了寫,瀏覽器控制檯顯示「TypeError:a未定義」。 – 2014-10-27 17:52:20
有趣的是,在上面的代碼中沒有'a',所以我不知道它來自哪裏。但將它作爲字符串傳遞是確保在上下文之間傳遞的唯一途徑。 – Noitidart 2014-10-27 20:04:41
我認爲我做錯了。 'var pic = $('。add-img'); var myobjStr = JSON.stringify(pic);' then 'var myobjFromStr = JSON.parse(myobjStr); $('body')。append(myobjFromStr);' 我不能使用這個對象,我錯了? – 2014-10-27 20:09:41