javascript - 在循环内时,我的 jQuery 函数不会应用于元素
问题描述
维度数组 -
var dimensions = [
{
"x": 0,
"y": 0,
"width": 680,
"height": 294.75
},
{
"x": 680,
"y": 0,
"width": 680,
"height": 294.75
},
{
"x": 0,
"y": 294.75,
"width": 680,
"height": 294.75
},
{
"x": 680,
"y": 294.75,
"width": 680,
"height": 294.75
}
]
创建窗口的窗口属性函数
function windowProperties(containment, x, y, width, height) { //New Window Style and jQuery Functions
$(containment).append('<div id="window' + divCount + '"><p id="windowName' + divCount + '"></p><p id="para' + divCount + '">Source</p></div>');
nWindow = document.getElementById('window' + divCount);
paragraph = document.getElementById('para' + divCount);
windowName = document.getElementById('windowName' + divCount)
paragraph.style.color = "black";
paragraph.style.fontSize = "20px";
paragraph.style.fontWeight = "bold";
paragraph.style.padding = "20px";
paragraph.style.textAlign = "center";
windowName.style.color = "black";
windowName.style.fontSize = "20px";
windowName.style.fontWeight = "bold";
windowName.style.padding = "10px";
windowName.style.textAlign = "center";
windowName.innerHTML = 'Window ' + divCount;
nWindow.style.width = width + "px"; //680
nWindow.style.position = "absolute";
nWindow.style.height = height + "px"; //294.75
nWindow.style.opacity = "0.5";
nWindow.style.background = "white";
nWindow.style.zIndex = "200";
nWindow.style.top = y + "px";
nWindow.style.left = x + "px";
$(function() {
$.fn.getPosition = function() {
var results = $(this).position();
results.right = results.left + $(this).width();
results.bottom = results.top + $(this).height();
return results;
}
$(nWindow).resizable({
maxWidth: 1356,
maxHeight: 585.5,
grid: [3, 4],
aspectRatio: true,
});
$(nWindow).draggable({
cursor: "move",
snapTolerance: '20',
snap: nWindow,
containment: '.videoWallContainer',
stop: function(e, ui) {
console.log($(this).attr("id"), $(this).getPosition());
}
});
$(sources).draggable({
cursor: "pointer",
helper: "clone",
appendTo: nWindow,
});
$(nWindow).droppable({
activeClass: "ui-state-default",
hoverClass: "ui-state-hover",
accept: sources,
drop: function(event, ui) {
$(paragraph).text(ui.draggable.text()).appendTo(nWindow);
console.log(nWindow);
}
});
});
}
一次创建所有元素
var createAllWindows = document.getElementById('createAllWindows').onclick = function() { //All window creation button
var i = 0;
while (i < dimensions.length) {
dimensions.forEach(function(size) {
i++;
console.log("Window " + i);
console.log(size.x);
console.log(size.y);
console.log(size.width);
console.log(size.height);
windowProperties(container, size.x, size.y, size.width, size.height); //Calling function
divCount++;
});
}
};
当在数组的各个位置创建窗口时,循环会创建它们,但不会将 jQuery 函数应用于窗口,即使它位于被调用函数内。我做错了什么吗?
解决方案
推荐阅读
- mysql - 包含按日期排序的列名的派生列
- c# - GemBox 文档将特殊字符添加到 boomark 中
- scala - 使用scala正则表达式提取[]内的所有字符串
- powershell - 更改 AD 用户组成员资格
- macos - 调用应用程序名称 FMX OSX
- hibernate - Hibernate 没有为具有支持属性的 Kotlin 属性创建连接表
- jenkins - 如何测试使用分支名称的 Jenkins 管道(或其他 jenkins 脚本)?
- reactjs - 使用 React hooks 和 memo 时如何防止子组件重新渲染?
- javascript - Javascript 将带参数的单例定义为模块
- cakephp - ACL 安装错误 - “异常:找不到插件 Acl”