使用 jquery 将事件附加到平面 ajax 响应 html

发布于 2024-09-13 08:57:54 字数 908 浏览 1 评论 0 原文

在我们的应用程序中,我们正在执行一些 AJAX 响应。有时我们会收到一个未完全包含下面代码中的测试 3 的响应。虽然可以让人们屈服,但我想在代码中添加一些腰带和吊带来处理这种情况。

关键是 findself() 函数,该函数旨在通过 $('...html...') 字符串获取任何响应 html 块,然后对其进行适当的处​​理。这仅适用于某些情况,但在情况 #3 中,它会因浏览器中的 getAttribute 错误而终止。

关于如何扩展 findself() 函数来处理情况 #3 有什么好主意吗?

var html;

function findself(sel, html) {
    return html.is(sel) ? html.find(sel).add(h) : html.find(sel);
}

html = $('<span rel="toggle">foo</span>');

findself('[rel="toggle"]', html).each(function () {
    console.log('ok 1');
});
console.log('done case 1');

findself('[rel="toggle"]', $('testing of a text node')).each(function () {
    console.log('ok 2');
});
console.log('done case 2');

html = $('<span>foo</span>some text<div rel="toggle">xx</div>');

findself('[rel="toggle"]', html).each(function () {
    console.log('ok 2');
});
console.log('done case 3');

In the application that we have, we're doing some AJAX response. Once in a while we get back a response that isn't fully contained Test 3 in the code below. While it's possible to beat people into submission, I would like to just build in some belt and suspenders in the code to handle the case.

The key is the findself() function which is designed to take any response html block via the $('...html...') string and then do the appropriate magic to it. This works just find for some cases, but in case #3 it dies with a getAttribute error in the browser.

Any good ideas of how to extend the findself() function to handle case #3?

var html;

function findself(sel, html) {
    return html.is(sel) ? html.find(sel).add(h) : html.find(sel);
}

html = $('<span rel="toggle">foo</span>');

findself('[rel="toggle"]', html).each(function () {
    console.log('ok 1');
});
console.log('done case 1');

findself('[rel="toggle"]', $('testing of a text node')).each(function () {
    console.log('ok 2');
});
console.log('done case 2');

html = $('<span>foo</span>some text<div rel="toggle">xx</div>');

findself('[rel="toggle"]', html).each(function () {
    console.log('ok 2');
});
console.log('done case 3');

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

软的没边 2024-09-20 08:57:54

快速版本:您可以通过确保文本元素不在两个元素之间且位于根元素之间来回避当前问题,如下所示:

function findself(sel, html) {
    var h = $("<div />").append(html);
    return h.is(sel) ? h.find(sel).add(h) : h.find(sel);
}

您可以在此处进行测试


解释:这是 jQuery 核心中的一个错误。由于有开始/结束标记,它会将其检测为 HTML 字符串,但它不是单个元素。它有 3 个节点,其中一个是文本,由于这种不正确的检测,它错误地将其视为元素字符串,而不是子元素的集合,例如:

jQuery.filter('[rel="t"]', $('<span>foo</span>some text'));            //works
jQuery.filter('[rel="t"]', $('<div><span>foo</span>some text</div>')); //works
jQuery.filter('[rel="t"]', $('<span>foo</span>text<div>xx</div>'));    //fails

所以目前, jquery.filter() (其中 .is() 使用)不适用于中间的文本节点。

Quick version: You can side-step your current issue by ensuring that text element isn't in-between 2 ones and at the root, like this:

function findself(sel, html) {
    var h = $("<div />").append(html);
    return h.is(sel) ? h.find(sel).add(h) : h.find(sel);
}

You can test it out here.


Explanation: This is a bug in jQuery core it looks like. It's detecting it as a HTML string, because of the opening/closing tags, but it's not a single element. It's 3 nodes one being a text, due to this incorrect detection it's incorrectly treating it as an element string, rather than a collection of children, for example:

jQuery.filter('[rel="t"]', $('<span>foo</span>some text'));            //works
jQuery.filter('[rel="t"]', $('<div><span>foo</span>some text</div>')); //works
jQuery.filter('[rel="t"]', $('<span>foo</span>text<div>xx</div>'));    //fails

So currently, jquery.filter() (which .is() uses) doesn't work with a text node in the middle.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文