如何使用switch语句设置变量的值,该语句的表达式是作为dom元素id设置的变量
How do I set the value of a variable using a switch statement with an expression that is a variable set as a dom elements id?
我遇到的问题是,我试图设置一个变量(contentUrl),我可以用它来指定我的ajax注入dom元素的URL。因此,当我拖放一个项目时,它会通过".load()"方法从另一个页面加载另一个元素。目前,我正试图使用一个开关语句来设置我的contentUrl变量。
另一个争论点是.load()方法,正如jQuery API中指定的那样,要求url是字符串。
我能做些什么来设置我的contentUrl基于CurDragItem的值,然后执行一个ajax .load方法返回一个元素从一个页面与contentUrl的值?如果这是不可能的开关和。load方法-我假设它不是-我怎么能实现我刚才描述的?
如果你需要我把它放在小提琴里,请告诉我。我再次使用jQuery UI的dnd小部件来完成这些无意义的内容。
下面是我的代码片段:$(function() {
var dragItems = $('.sidebar-listItem');
var dropArea = $('.drop');
var sidebarWrap = $('#l-sidebar-wrapper');
var sidebarList = $("#l-sidebar-list");
var contentList = $('.main-content-list');
dragItems.draggable({
cursor: "move",
cursorAt: {top:60, left:45},
helper: 'clone',
revert: "invalid",
scroll: false,
appendTo: 'body'
});
// setter
dragItems.draggable( "option", "scroll", false );
dropArea.droppable({
accept: ".sidebar-listItem",
activeClass: "dogs",
hoverClass: "dogs",
drop: function() {
//set the curDragItem value to the id of the element that is being dragged
var curDragItem = $('.ui-draggable-dragging').attr("id");
//initialize the variable with the value of nothing
var contentUrl = "nothing";
//use switch statement to check whether curDragItem matches a case and then set value of contentURL
switch (curDragItem) {
case 'hour24-1':
contentUrl = "hour24.html";
break;
case 'matisyahu':
contentUrl = "matisyahu.html";
break;
case 'dirtyheads':
contentUrl = "dirtyHeads.html";
break;
}
//check what the value of contentUrl is
console.log(contentUrl);
//load the .main-content-list from a page with the url that is the value of contentUrl
dropArea.load("value of contentUrl .main-content-list", initDynamicEventHandlers);
}
});
});
正如dandavis在评论中提到的那样,您最好的选择可能是对象映射器,以防您需要在应用程序的其他地方使用它。
除此之外,你还可以像jQuery UI文档中提到的那样,为drop函数添加参数,这样你就可以访问任何被删除的对象。
var dropArea = $('.drop');
var sidebarWrap = $('#l-sidebar-wrapper');
var sidebarList = $("#l-sidebar-list");
var contentList = $('.main-content-list');
var mapper = {
'hour24-1': 'hour24.html',
'matisyahu': 'matisyahu.html',
'dirtyheads': 'dirtyHeads.html'
};
dragItems.draggable({
cursor: "move",
cursorAt: {top:60, left:45},
helper: 'clone',
revert: "invalid",
scroll: false,
appendTo: 'body'
});
// setter
dragItems.draggable( "option", "scroll", false );
dropArea.droppable({
accept: ".sidebar-listItem",
activeClass: "dogs",
hoverClass: "dogs",
drop: function(event, ui) {
//set the curDragItem value to the id of the element that is being dragged
var curDragItem = ui.attr("id"); // Use the passed in parameter
//initialize the variable with the value of nothing
var contentUrl = "nothing";
//check the value of curDragItem
console.log('Drag item: ' + curDragItem);
contentUrl = mapper[curDragItem];
//check what the value of contentUrl is
console.log(contentUrl);
//load the .main-content-list from a page with the url that is the value of contentUrl
dropArea.load(contentUrl + " .main-content-list", initDynamicEventHandlers);
}
});
});
...
相关文章:
- 在if语句下的html中使用javascript变量
- Javascript变量到PHP变量-如果语句不起作用,则发布
- 在if语句注释的条件内指定变量的值
- jade/pug是否可以在include语句中使用变量
- for 循环和 if 语句,获取变量并存储它
- 在Javascript中将条件语句转换为变量
- 空变量的 else 语句
- 如何使用If语句更改JavaScript变量的值
- 在Javascript中的变量内部使用while()语句
- 缩短同一变量的多个if语句
- JavaScript: 变量赋值 in for 语句 |在前面或里面
- 在JavaScript / jQuery中,是否有一种更短的方法可以编写在同一个测试变量上具有许多OR的“if”语句
- SELECT FROM WHERE语句在使用变量时不起作用…
- 全局变量和if语句与此的比较
- 使用带有两个变量的相同if-else语句
- JavaScript 将变量中的 && 语句组合为真或假
- 在if语句中定义JavaScript变量
- 将数组放入if语句的变量中
- 每个语句中的jQuery全局变量
- Javascript -为什么变量语句被执行