【发布时间】:2016-03-25 08:56:59
【问题描述】:
我正在尝试使用 Jquery 构建一个 Web 应用程序。我创建了一个函数并在全局范围内声明它,在调用它之后我正在使用 this.each(function () 并尝试执行我没有得到所需结果的函数要求。我也检查了语法并且没有收到任何错误。 以下是我调用该函数的代码:
$('[data-nitspagelabel]').click(function () { //click function on editable div to get editable buttons
var nitsedit = $(this);
var labeltype = $(this).data("nitslabeltype");
if (labeltype == "text") {
if (modal == false) {
modal = true;
$('[data-nitstextbutton]').css({ //popup text editing buttons
'top': mouseY
, 'left': mouseX
}).fadeIn(400).click(function (e) {
var popupbox = $(this).attr('href');
openPopup(popupbox); // opens the editing tools popup
$(nitsedit).nitspopupeditor; // formatting buttons in action
$(nitsedit).attr('contenteditable', 'true');
});
}
}
});
以下是我全局声明的函数
$.fn.nitspopupeditor = function () { //Function to format editable items.
this.each(function () {
setTimeout(function () {}, 100);
var $this = $(this);
var selected_text = null;
var uniquid = "nits" + new Date().getTime();
$this.attr('data-nitsselect', uniquid); //set element unique id
我不知道我在哪里做错了,但是函数内部的东西不能正常工作。
【问题讨论】:
-
this在你的函数里面是window,你想循环什么? -
你的意思是,我需要在
$.fn.nitspopeditor的第二行将this替换为window。 -
this里面每个不是 eqwindow。据我所知,this里面每个都是当前循环值。 -
不,我的意思是
this将指向window实例。 -
@VasylMoskalov 我说的是
this代码中的this.each