- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试使用 jquery 函数 replaceWith() 将具有“问题”类的现有 div 替换为相同的 div,只是按字母顺序排序。
排序有效,替换似乎有效,尽管它们似乎被无限次替换,而我只想将它们替换一次。
我想用类“.question”替换那些具有相同 div 的 div,尽管只是用
排序请参阅下面的原始 HTML 屏幕截图。请注意,有 31 个 div。
这是我的javascript代码:
$(document).ready(function()
{
// Sorting function
function getSorted(selector, attrName)
{
return $($(selector).toArray().sort(function(a, b)
{
var aVal = parseInt(a.getAttribute(attrName)),
bVal = parseInt(b.getAttribute(attrName));
return aVal - bVal;
}));
}
var sortedItems = getSorted('div.question', 'data-answerCount').clone();
var unsortedQuestionItems = $('.question');
console.log("Replacing unsorted question items with sorted question items");
console.log("oldArray is of length: " +unsortedQuestionItems.length);
console.log("newArray is of length: " +sortedItems.length);
unsortedQuestionItems.replaceWith(sortedItems);
console.log("Replacing completed.");
var afterReplaced = $('.question');
console.log("After replacing, the length of .question is: " +afterReplaced.length);
}); //ends the document.ready function
当我尝试加载页面时,它(看似)无限次地替换了那些 div,控制台输出如下:
排序似乎有效,但我不希望它被无限追加多次!我只想对这 31 个 div 进行排序和显示一次。有什么想法吗?
最佳答案
您代码中的主要问题出在这两行:
(1) var unsortedQuestionItems = $('.question');
...
(2) unsortedQuestionItems.replaceWith(sortedItems);
在 (1) 上,您选择具有类 question 的每个元素(这些可以是很多项目,就像您说的),然后在 (2) 您正在将 replaceWith() 方法应用于这些项目中的每一个,因此具有类 question 的每个项目都将被替换为排序后的数组元素,这就是您多次看到排序数组的原因。我给你一个固定的例子,解决了这个问题并做了一些其他的修复。
$(document).ready(function()
{
// Define the sorting function
// NOTE: sort() is applied without converting to array.
function getSorted(selector, attrName)
{
return $(selector).sort(function(a, b)
{
var aVal = parseInt(a.getAttribute(attrName));
var bVal = parseInt(b.getAttribute(attrName));
return aVal - bVal;
});
}
// Get sorted and unsorted elements.
var sortedItems = getSorted('div.question', 'data-answerCount');
var unsortedQuestionItems = $('.question');
console.log("Replacing unsorted question items with sorted question items");
console.log("oldArray is of length: " + unsortedQuestionItems.length);
console.log("newArray is of length: " + sortedItems.length);
// Replace old unsorted elements with sorted elements.
unsortedQuestionItems.parent().empty().append(sortedItems);
console.log("Replacing completed.");
var afterReplaced = $('.question');
console.log("After replacing, the length of .question is: " + afterReplaced.length);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="container questions-container">
<div class="question" data-answercount="16">16</div>
<div class="question" data-answercount="4">4</div>
<div class="question" data-answercount="10">10</div>
<div class="question" data-answercount="4">4</div>
<div class="question" data-answercount="5">5</div>
</div>
关于javascript - replaceWith() 函数似乎无限次运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52960951/
我正在制作一个包含大量空白 div 标签的简单网页 在我的 css 中,我将 div 设置为 100px x 100px 的正方形,并带有背景色。 我正在使用 jQuery 让鼠标通过改变颜色与 d
我看到了在链接功能中放租元素的其他示例 例子一: var template = ''; element.html(template); $compile(element.con
我的网站使用 jQuery 1.4.2。问题是 .replaceWith() 在 jQuery 1.4.2 的 IE6 和 IE7 中不起作用。 jQuery 1.4.2 中是否有 IE6 和 IE7
我正在尝试用 jquery ReplaceWidth() 替换一段 HTML yo 像这样: $('#content-box').replaceWith(response.box); 但后
我也尝试在其他问题上搜索此内容,但我似乎不理解这种方法。我正在尝试在图像顶部创建标题,如果文本长度超过 40 个字符,脚本应减少文本的长度,并在字符串末尾添加 3 个点 let content = d
如果在加载网站时创建文本字段,我可以使 jQuery 日期选择器工作,但如果我使用 .replaceWith() 函数使其显示,则它将无法工作。 这是我在网站加载时出现的代码: http://jsfi
我有一个如下所示的a: click to change 现在我想将其替换为另一个标签,例如 p。我尝试了这个但不起作用: function f1(tor) { tor.rep
是否有类似 replaceWith() 的 jQuery 函数,可以用新元素替换元素,而不破坏原始元素(只是隐藏它)。 最佳答案 你可以这样做: replace your first content
我正在尝试使用 .replaceWith 将单词替换为函数,但它不起作用我认为问题是我没有正确地正确编写函数,但我不知道如何正确编写它。任何人都可以帮助我吗(只是很小的一部分。)谢谢。
我有一个界面,我想通知用户使用 invoke运算符而不是 oldFunction . interface SomeInterface { operator fun invoke(param: A)
我有 3 个 div,我想用另一个 div 的开始标签替换第一个 div,用结束标签替换第三个 div。这就是我的意思: 1 2 3 当我尝试用替换(使用replaceWith())第一个div时第三
我需要单击一个列表项,将 div 中的文本替换为 xml 文件中节点中的文本。 我单击一个列表项并将结果存储在一个变量中。然后,我加载 xml,找到所需节点的标题,并将结果作为一个变量。然后我运行一个
我正在尝试使用 jquery 函数 replaceWith() 将具有“问题”类的现有 div 替换为相同的 div,只是按字母顺序排序。 排序有效,替换似乎有效,尽管它们似乎被无限次替换,而我只想将
我试图在 jQuery 中使用 replaceWith() 来替换我表中的一行,但它并没有替换。添加了新行,旧行仍然存在(直到我刷新页面)。我一定是遗漏了一些东西,因为从文档来看这应该可以工作....
所以我在替换我的 js/jQuery 代码上的空输出时遇到了一些麻烦,它正在替换每个文本,即使它是有效的。 没有 if (items[z].Office == null) { 语句: 使用 if 语句
我有以下标记: Headline Events...
我正在为我正在设计的 web 应用程序使用 jquery 的 .replaceWith() 函数。我的 HTML 是这样的: {% for service in services %}
这个脚本的第二个“点击”功能不起作用,我认为这只是一个语法问题。我对此很陌生。但我需要找到一种简单的方法,为变量数组调用一次实时“click”函数。 脚本可以工作,只是第二部分不行。 我正在寻找一种有
我不想在委托(delegate)中使用 live() 或 on() :/ 是否有其他方法可以替换元素的 HTML 代码但保留元素数据? 最佳答案 当您通过 JavaScript 替换 HTML 元素时
我有以下 jQuery 可以用选择字段替换输入字段。 input = $("input[id$='id_var']")[0]; input_class = input.getAttribute("cl
我是一名优秀的程序员,十分优秀!