- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
Note: I've solved major issues with this question however I still do not have working code, since what I am doing now is a completely different issue I will open a new question solve it there and then post the answer to this question. I'll also add the link to the question. You can see my current progress here: http://jsbin.com/upatus/2/edit
我正在编写两个基本的 jQuery 插件,$.fn.query
和 $.fn.build
,它们对数组进行排序,并创建要插入的 html 代码分别是一个文档。我目前正在使用 Vimeo 视频 ID 进行测试,我将使用它来显示视频。
$.fn.query
工作正常,但我在以下代码中遇到错误
$.fn.build = function(params) {
// Parameters //
var options = $.extend( {
'wrapAll' : undefined,
'splitBy' : undefined,
'wrapRow' : undefined,
'wrapEach' : '<div>'
}, params),
build;
// Wrap Each //
if (options.wrapEach !== undefined) {
build = this.wrap(options.wrapEach);
}
// Split into Rows //
if (options.splitBy !== undefined && options.wrapRow !== undefined) {
var tmp;
for (var i = build.length; i > 0; i -= 3) {
tmp = build.splice(i, i + 3).wrapAll(options.wrapRow);
}
build = tmp;
}
// Wrap All //
if (options.wrapAll !== undefined) {
build = build.wrapAll(options.wrapAll);
}
// Return Build //
return build;
};
然后调用(两个)函数:
var query = $(example).query({
shuffle: true,
limit: 6
}).build({
wrapAll: '<div class="container" />',
splitBy: 3,
wrapRow: '<div class="row" />',
wrapEach: '<div class="span4" />'
});
导致以下错误
Uncaught Error: NOT_FOUND_ERR: DOM Exception 8
这并不完全有帮助,因为它显示了一个 jQuery 错误,该错误似乎存在于 million different places 中。 。
您可以在此处查看我在 jsFiddle 上的 javascript 代码
<小时/>
PS: I am trying to follow best practices at all times so if you notice anything even slightly off please let me know and I'll fix the code.
最佳答案
我认为您将 jQuery DOM 函数与数组操作混淆了。没有什么理由将两者链接在 jQuery 原型(prototype)中,因为操作非常独立。
此外,$.fn.build
并不是基于调用原型(prototype)之前创建的元素构建的,而是在其中执行一些 wrapAll
方法。为什么不引入一个外部容器并根据数据在其中构建 DOM 结构?
尝试这样的事情:
$.query = function(data, options) {
// do the array thingie with data
return data;
};
$.fn.build = function(data, options) {
return this.each(function() {
// do the DOM thingies based on data and the context element.
// don’t use wrapAll, bring an outside element instead
});
}
然后
$('<div>').addClass('container').build( $.query(example), {
splitBy: 3,
wrapRow: '<div class="row" />',
wrapEach: '<div class="span4" />'
}).appendTo('body');
您还可以伪装插件内的 $.query
调用:
$.fn.build = function(options) {
options.data = $.query( options.data );
// etc
$('<div>').addClass('container').build({
data: example,
splitBy: 3,
wrapRow: '<div class="row" />',
wrapEach: '<div class="span4" />'
}).appendTo('body');
关于javascript - jQuery 插件 .wrap() 和 .wrapAll() 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12222477/
我试图用另一个元素包装动态创建的一系列生成元素,例如: var innerHtmlAsText = "test1test2"; var wrapper = ""; $("div#target").ap
有一些 HTML 看起来像这样: Do all declarations for the same static function or vari
我坚持尝试将 3 个 div 封装在一个包装 div 中。到目前为止,我已经成功地完成了 2 个 div,但我无法弄清楚最后一个。 你可以在这里看到我的 JSFiddle:http://jsfiddl
我正在使用这个脚本来包装两个 div: jQuery: $("#wrapcb").click(function(){ $('#cboxOverlay, #colorbox').wrapAll(''
我有一组生成的 div 宽度相同的类,其中包含标题和一系列链接。由于某种原因,末尾有一个隐藏的 header 。 它们看起来像这样: List Header List item Li
考虑这段代码: var n = 5; // NOTICE THIS LINE!! $(function() { var $newDiv = $("#content > .post:gt(5)").w
假设这是我的 HTML 现在我想将 .product-thumbnail 和 .product-price 包装在 像这样
我正在使用 stackoverflow 上的一位成员建议的一些代码,并由我进行修改,将每 3 个列表项包装为大型菜单的一部分。代码是: var lis = $("ul > li"); for(var
考虑以下代码:(实例 here ) $(function() { var wrapper = $(""); $(".a").wrapAll(wrapper); wrapper.css("b
请看一下下面的 fiddle : http://jsfiddle.net/ardoramor/UzJqd/ 当 fiddle 加载时,带有 .wrapper 类的 DIV 向下滚动 200px。这包括
用 jQuery 包装 div 后,展开它们然后尝试再次包装它们似乎会丢失 float 属性(我的猜测),我不知道如何解决这个问题。 当第一次按下 JSFiddle 中的换行按钮时,它会像我期望的那样
如何确保所有带有 .px-sub-menu-item 的类都包含在一个 div 中? (function($) { $('.px-sub-menu-item').each(function() {
我有两个变量 currentSectionElement 和 sectionList。 它们看起来像: currentSectionElement = $("current"); sectionLis
我看到有 wrap() 的回调以及 wrapInner()但我注意到 wrapAll 没有回调函数. 这是否意味着操作以同步方式而不是异步方式发生? 如果我这样做是正确的吗? $('.test').w
我需要将三个 div 合并为一个,但是这三个 div 在代码中重复了好几次。也许我的 HTML 会解释: 我想要实现的是: 这是将所有内容包装到
我有一个容器 div,里面有两个 div,像这样: 我在“child1”和“child2”周围包裹了另一个 div,但它出现了两次,我无法修复: $(".child1, .child2
我的标记如下所示: 我希望事情是这样的: 因此我可以填充 #isolated
我需要找到所有 div 中具有 someClass 类的所有 p 标签,并将它们用另一个 div 包装。开始标记如下所示: // Lots of different tags generated
我有一系列 DIV,它们根据大小分配了 .big 或 .small。它们需要以两到三个为一组进行包装。实际上,分配的空间只能容纳两个 .big。 如果两个 .big DIV 彼此相邻存在,则应将它们分
我有这个 date-picker 框,我使用 wrapAll 添加了一个日历图像,下图是结果: 这是我的代码: $(function() { $(".date-picker").datepicke
我是一名优秀的程序员,十分优秀!