在 .load() 方法检索的 HTML 中查找元素时出现问题
Issue finding elements in HTML retrieved by .load() method
我在尝试动态生成HTML中超链接的侧边栏索引时遇到了一个有趣的怪癖,该索引已通过.load()
方法放入div中。
该脚本查找适当的锚标记并在旁边复制它们,但在更新 DOM 之前.load()
占用元素的内容中找到它们。
相关JS:
var $result = $("#result"); // optimize!
var $aside = $("aside");
var $head = $("head");
var $title = $("title");
$.getJSON("script.php", queryObject, function(data) {
$("#newStyle").remove(); // get rid of old specific styles
$.each(data, function(i, row) {
$result.load(row.html); // the database is storing a bunch of urls
$title.text(row.title);
if (!!row.css) { // get specific styles if they exist
$("<link/>", {
rel : "stylesheet",
id : "newStyle",
href : row.css
}).appendTo($head);
}
if (!!row.js) $.getScript(row.js); // get specific scripts if they exist
});
var $links = $result.find("a").not(".toTop a"); // I have "back to top" things to avoid
if ($links.length) { // better way to do this? this always returns true.
var string = "<h2>References</h2><ul>";
$links.each(function() {
var $this = $(this);
string += "<li><a href='" + $this.attr("href") + "'>" + $this.text() + "</a></li>";
});
string += "</ul>";
$aside.html(string);
}
}).fail(errorHandle); // ain't no scrub... probably am scrub
这会导致在触发$.getJSON()
和后续 DOM 更新的事件之前填充页面中的锚点列表(符合描述)。试图在 JSFiddle 中重现该问题,但它与 .load()
配合不佳。
有没有办法确保$links
将是一个充满新鲜锚点的对象?
正如@Rhumborl所指出的,问题是$.load()
的异步性。它变得有点粗糙,因为我希望列出内容的顺序以及 JSON 从 2 个数据库查询构建的方式不太好。这是有效的函数:
function anchorSelf() {
$.getJSON("/php/loadsop.php", {"query" : $(this).attr("value")}, function(data) {
// reset CSS from current page
$("#newStyle").remove();
$.each(data.sop, function(i, row) {
// build aside heading
id = row.sopID;
if (row.sopID < 100) id = "0" + id;
if (row.sopID < 10) id = "0" + id;
string = "<h1>SOP " + row.catID + id;
if (!!row.suffix) string += row.suffix;
if (!!row.revision) string += " rev " + row.revision;
string += "</h1><h2>" + row.category + "</h2>";
$title.text(row.shortTitle);
if (!!row.css) {
$("<link/>", {
rel : "stylesheet",
id : "newStyle",
href : row.css
}).appendTo($head);
}
if (!!row.js) $.getScript(row.js);
});
// start the list of sops in the category
string += "<ul>";
$.each(data.links, function(i, row) {
string += "<li><a class='self' value='" + row.sopID + "'>" + row.shortTitle + "</a></li>";
});
string += "</ul>";
$.each(data.sop, function(i, row) {
// load the new page
$result.load(row.html, function() {
// find references in the new html
var $links = $result.find("a").not(".toTop a").not("a[name]");
if ($links.length > 0) {
string += "<h2>References</h2><ul>";
$links.each(function() {
$this = $(this);
if ($this.hasClass("self")) string += "<li><a class='self' value='" + $this.attr("value") + "'>" + $this.text() + "</a></li>";
else string += "<li><a href='" + $this.attr("href") + "'>" + $this.text() + "</a></li>";
});
string += "</ul>";
}
$aside.html(string);
});
});
$aside.html(string);
}).fail(errorHandle);
$result.on("click", "a.self", anchorSelf);
$aside.off("click", "a", catQuery).on("click", "a.self", anchorSelf);
}
总之,我在第二个键的$.each
之后为第一个键添加了另一个$.each
,以便在回调函数中string
占位符变量的连接将在侧边栏中产生所需结果的位置处理.load
。
相关文章:
- 如何使用jquery迭代具有相同属性的html元素并查找onclick事件
- 如何在不传递此信息的情况下查找被调用的元素
- 查找元素高度,包括边距
- 为什么可以't PHP查找可以使用JS或jQuery访问的元素
- 查找数组javascript中包含的元素类型
- 在AngularJs中查找元素并更改其内容
- JavaScript-如何按类查找元素并删除此类
- 在Meteor中如何查找DOM元素(渲染后)
- jquery在元素中查找文本节点,并使用标记进行连接和包装
- 如何按id查找两个类中任一类的子元素
- 从json数组中查找满足条件的特定元素的值
- 如何通过来自不同父元素的相同元素查找 HTML 元素
- 为动态创建的元素查找前一个同级的值
- HTML5 音频元素 - 查找滑块 - 无法在“HTMLMediaElement”上设置“当前时间”属性:提供的双精度值
- 具有用户添加的行的窗体,以及对每行的输入元素查找
- 选择tr及其“所有子元素”;-查找jquery选择器
- 为每个元素查找data()键值对
- 如何根据兄弟元素中排序的元素查找索引
- 为多类元素中某一类的元素查找Dom节点索引
- 如何使用多行元素查找offsetWidth