- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
在下面的示例中,我设置一个对象数组并按关键参数(位置)对它们进行排序。然后我需要输出结果 - 但如何输出?请参阅代码中的注释。
$(document).ready(function () {
function sortHooks(){
// add modules
var modules = [
{ module: 'navbar', hook: 'hook-header', position: '2'},
{ module: 'logo', hook: 'hook-header', position: '3'},
{ module: 'description', hook: 'hook-header', position: '1'}
];
// sort by "position" ASC
function byPosition(a,b) {
if (a.position < b.position)
return -1;
if (a.position > b.position)
return 1;
return 0;
}
// jQuery function with the given parameters
/* for each modules as modules
for each hook as hook
do the following:
$( '#' + module )prependTo( '#' + hook )
so for the given example it should return
$('#description')prependTo('hook-header')
$('#navbar')prependTo('hook-header')
$('#logo')prependTo('hook-header')
so they can be executed in that specific order.
And since we're using "prepend" that will mean
that the items be placed like this:
#logo
#navbar
#description
*/
return ???
}
// Execute sortHooks function
sortHooks();
});
也许是这样的,但我不太确定:
$.each(modules, function(index, value) {
$( '#' + value.module ).prependTo( '#' + value.hook ));
});
我还尝试了很多其他方法,其中之一是 Hook elements with insertAfter, but stay in parent - jQuery ,效果很好,但是如果位置高于父级中的子级,它不知道如何处理它并导致很多不需要的行为。
最佳答案
只需使用常规循环
for(var i=0; i<modules.length; i++){
$( '#' + modules[i].module )prependTo( '#' + modules[i].hook );
}
关于javascript - 如何将 JavaScript 对象数组转换为 jQuery prependTo,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22542709/
我正在构建一个网站进行实验,但我在使用某些代码时遇到了问题。 下一个按钮可以工作,但上一个按钮不起作用。它基本上将前两个 div 与类“tri”交换,但不交换另一个。 我不明白为什么一个有效而另一个无
我有这个代码: Text Text Text Text 当我将 span 与 selected 类的链接移动到第一个位置时,我这样做: $('.selected')
如何在 $(this) 的具体后代中添加一个类,例如我只想添加到本例中实际单击的 div 之前... $('div').click(function(){ $('hello').p
我有以下 Hmtl: Titile
我正在尝试重组元素。我面临的问题是元素没有唯一的类或 ID。因此,例如,如果 .prependTo被调用时,它显然会移动所有匹配的元素。 HTML 看起来像这样(重要:真实场景包含数百个 元素):
var orgin = $( ".images > a" ).clone(); $(orgin).prependTo( ".images .thumbnails .slideshow").wrap('
我正在为户外动力设备经销商创建一个显示构建器应用程序,经销商可以在其中添加、删除和重新排列不同的显示部分。 显示部分列表位于名为 的 div 中部分控件。这个div包含左右两列(分别命名为leftCo
出于某种原因,jQuery 中的 prependTo() 函数对我不起作用。我可能缺少一些简单的东西,但我根本看不到它。我基本上只是想通过 jQuery 让最后一张图像出现在开始时。 $(docume
我编写了以下代码,旨在创建一个 div 并在其中创建一个嵌套的跨度。这是代码。它的结果让我感到困惑: function(){ $("", { text: "SomeText",
似乎找不到这个问题的答案。 我的代码是这样的: var new_img = ''; var drop_img = '' + new_img + ''; $(drop_img).hide().prepe
我有一个与此类似的问题: PrependTo item only ONCE in Jquery 但是我的 DOM 结构是: 我希望使用 jQuery 的输出是:
我有这样的代码: Lorem Ipsum Lorem Ipsum dolor si amet... Lorem Ipsum
我在理解以下行为时遇到了一些麻烦。我有一个容器 其中包含一些内联 block 节点。示例 View : 现在我的要求是,添加新的 foobar 内联 block 元素。没问题,使用 jQuery ->
jquery 快速问题: 我知道appendTo和prependTo,但是可能有类似htmlTo的东西吗?是的,我知道这听起来很愚蠢,但我不想添加像appendto和prependTo这样的元素,我想
我有一个元素列表,例如:.title,我将它们克隆出来并将它们附加到容器中,但顺序与 prependTo` 相反。 如何将这些克隆元素放入数组并在添加它们之前反转它? 例如,对于每个 .title 克
我有一个关于 jQuery prependTo 的问题。 这是html框架,我无法更改框架。 press title 1 Fraser Hughes Evening Times Oc
我正在制作一个项目,在某些行中,有一个检查按钮,检查时会将其父项重新定位到底部。这是它的标记:
我有一个小问题,我在 div 中有一些静态内容,我需要向其中添加一些额外的内容,prependTo 效果很好,但新内容出现在现有内容之后。appendTo 出现在现有内容之前,但每个新追加的内容都出现
现在是午夜,我应该去 sleep ,但我完全被蒙蔽了。我知道我在这里会感觉像一个完全的蛋,但是......为什么这不起作用?? var galleryPanelText = ''; galleryPa
我有一些代码。当它正确运行时,我创建一条成功消息并使用 prependTo 以便在屏幕上显示它。问题是,当用户执行两次成功操作时,我最终会收到两条成功消息。 有没有办法覆盖我的成功消息。这是我目前在我
我是一名优秀的程序员,十分优秀!