将 HTML 字符串转换为 jQuery 对象然后解析它时出现问题

发布于 2024-12-29 09:09:15 字数 1289 浏览 0 评论 0原文

我设置了一个小片段,它将从 textarea 获取用于电子邮件营销的自动回复代码,当 textarea 失去焦点时,它会解析刚刚粘贴的内容并从中获取一些属性和元素,然后将它们位于单独的输入字段中。

问题是我编写的代码显然没有正确执行此操作,对于某些自动应答器代码,它不会抓取表单的操作 URL,有些它会抓取所有隐藏的输入字段,通常没有问题,然后有时不想抓取姓名或电子邮件字段的“姓名”属性。

代码非常简单。我尝试使用 .find() 而不是 .filter() ,它似乎只是随机起作用。有些功能可以在 .find() 中使用,但有些功能可以在 .find() 中使用。我认为这不是问题的根源,但可能有助于解释正在发生的事情。

编辑它似乎与.find()一起工作“更好”

我只是不知道为什么它会这样,有什么想法吗?

插入文本区域的 HTML 表单示例在这里。 (它很乱,但不是我写的!) -- 这段代码将位于 textarea 中,其中下面的代码在失去焦点时对其进行解析。

编辑:问题的根源似乎是

部分。

最新的 JSFiddle <- 代码在这里

I have set up a little snippet that will take an autoresponder code for email marketing from a textarea and when the textarea loses focus, it parses what was just pasted and grabs some attributes and elements from that, and puts them in separate input fields.

The problem is that the code I've written apparently doesn't do this right and for some autoresponder codes, it won't grab the form's action URL, some it does, it usually has no problem grabbing all of the hidden input fields, and then sometimes does not want to grab the name or email fields 'name' attributes.

The code is pretty straight forward. I tried using .find() instead of .filter() and it seems to just act at random. Some things work that didn't work with .find(), some things don't work that did work with .find(). I don't think that's the root of the problem but may help to explain what's going on.

Edit It seems to work "better" with .find().

I just have no idea why it would behave like this, any ideas?

Example form HTML that is inserted to a textarea is here. (It is messy but I didn't write it!) -- This code would be in a textarea in which the code below parses it when it loses focus.

Edit: The root of the problem seems to the the <form action="#"> part.

Most recent JSFiddle <- code here

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

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

发布评论

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

评论(1

千秋岁 2025-01-05 09:09:15

我相信 html 的问题不像您的 AWeber 示例那样工作,而是它不包含在单个顶级父元素中,这意味着当您解析它以创建 arcode jQuery 时object 该对象包含

元素作为顶级元素。这反过来意味着 .find() 方法(查找后代元素)找不到任何内容,因为没有其他 元素是子元素。 (您可以通过检查 arcode.length 或使用 arcode.each() 记录 arcode 中元素的标记名来自行测试。 )

您的示例确实有效,恰好包含在顶级

中,因此它没有此问题。

因此,我建议的修复方法就是将输入的 html 自己包装在

元素中,从而使所有内容都可以与 .find() 一起使用。然后简化您的功能,因为以下简短版本可以完成您原始功能所做的所有工作:

$j( '#jwsqz_autocode' ).blur( function() {

    var thehtml = "<div>" + $j( this ).val() + "</div>";
    var arcode = $j( thehtml );

    $j('#jwsqz_autoformurl').val( arcode.find('form').attr('action') );
    $j('#jwsqz_arnameinput').val(
         arcode.find('input[type="text"][name*="name"]').attr('name') );
    $j('#jwsqz_aremailinput').val(
         arcode.find('input[type="text"][name*="email"]').attr('name') );

    var hiddens = "";

    arcode.find( 'input[type="hidden"]' ).each( function() {
       hiddens += $j( this ).prop( 'outerHTML' ) + "\n";
    });

    $j( '#jwsqz_autohidden' ).val( hiddens );

});

工作演示: http:// jsfiddle.net/fpK9F/6/ (似乎可以处理您的示例案例)。

您会注意到我使用了 jQuery 属性包含选择器[名称*=“电子邮件”]

请注意,如果代码中的以下测试是多余的:

if( hiddens != '' )
   $j( '#jwsqz_autohidden' ).val( hiddens );
else
   $j( '#jwsqz_autohidden' ).val( '' );

else 情况下,您无论如何都要设置 hiddens 变量中的值,因此您可以替换整个仅包含 if 部分的结构 - 正如我在上面的代码中所做的那样。

更新:对于 name 属性值的不区分大小写的测试,您可以这样做:(

    $j('#jwsqz_aremailinput').val(
         arcode.find('input[type="text"]')
               .filter(function(){ return /email/i.test(this.name); })
               .attr('name') );

您不必使用正则表达式,关键点是 .filter()< /code> 函数。)

不区分大小写的演示: http://jsfiddle.net/fpK9F/7/

I believe the problem with the html that doesn't work like your AWeber example is that it is not contained within a single top-level parent element, which means that when you parse it to create the arcode jQuery object that object contains the <form> element as a top level element. That in turn means that the .find() method - which looks for descendent elements - doesn't find anything since there are no additional <form> elements that are children. (You can test this for yourself by checking arcode.length or using arcode.each() to log the tagnames of the elements in arcode.)

Your example that did work happened to be wrapped in a top-level <div>, so it didn't have this problem.

So my suggested fix for this is just to wrap the entered html in a <div> element yourself, thus making everything work with .find(). And then simplify your function, since the following short version does all that your original function did:

$j( '#jwsqz_autocode' ).blur( function() {

    var thehtml = "<div>" + $j( this ).val() + "</div>";
    var arcode = $j( thehtml );

    $j('#jwsqz_autoformurl').val( arcode.find('form').attr('action') );
    $j('#jwsqz_arnameinput').val(
         arcode.find('input[type="text"][name*="name"]').attr('name') );
    $j('#jwsqz_aremailinput').val(
         arcode.find('input[type="text"][name*="email"]').attr('name') );

    var hiddens = "";

    arcode.find( 'input[type="hidden"]' ).each( function() {
       hiddens += $j( this ).prop( 'outerHTML' ) + "\n";
    });

    $j( '#jwsqz_autohidden' ).val( hiddens );

});

Working demo: http://jsfiddle.net/fpK9F/6/ (seems to handle your example cases).

You'll notice I've made use of the jQuery attribute contains selector with [name*="email"].

Note that if tests like the following from your code are redundant:

if( hiddens != '' )
   $j( '#jwsqz_autohidden' ).val( hiddens );
else
   $j( '#jwsqz_autohidden' ).val( '' );

In the else case you are setting the value that was in the hiddens variable anyway, so you can replace the whole structure with just the part from the if - as I did in my code above.

UPDATE: For a case-insensitive test of the name attribute's value you can do this:

    $j('#jwsqz_aremailinput').val(
         arcode.find('input[type="text"]')
               .filter(function(){ return /email/i.test(this.name); })
               .attr('name') );

(You don't have to use a regular expression, the key point is the .filter() function.)

Case insensitive demo: http://jsfiddle.net/fpK9F/7/

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