将 HTML 字符串转换为 jQuery 对象然后解析它时出现问题
我设置了一个小片段,它将从 textarea
获取用于电子邮件营销的自动回复代码,当 textarea 失去焦点时,它会解析刚刚粘贴的内容并从中获取一些属性和元素,然后将它们位于单独的输入字段中。
问题是我编写的代码显然没有正确执行此操作,对于某些自动应答器代码,它不会抓取表单的操作 URL,有些它会抓取所有隐藏的输入字段,通常没有问题,然后有时不想抓取姓名或电子邮件字段的“姓名”属性。
代码非常简单。我尝试使用 .find()
而不是 .filter()
,它似乎只是随机起作用。有些功能可以在 .find()
中使用,但有些功能可以在 .find()
中使用。我认为这不是问题的根源,但可能有助于解释正在发生的事情。
编辑它似乎与.find()
一起工作“更好”。
我只是不知道为什么它会这样,有什么想法吗?
插入文本区域的 HTML 表单示例在这里。 (它很乱,但不是我写的!) -- 这段代码将位于 textarea
中,其中下面的代码在失去焦点时对其进行解析。
编辑:问题的根源似乎是
例如,Aweber 代码将不会检索表单 URL(
action
属性)Ebizac 代码 不会检索表单 URL
Imnica 代码 现在显然工作正常了(?)。
GetResponse Code 没有问题。
最新的 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.
An Aweber Code for example, will not retrieve the Form URL (
action
attribute)An Ebizac Code will not retrieve the Form URL
An Imnica Code works fine now apparently(?).
There's no problems with the GetResponse Code.
Most recent JSFiddle <- code here
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我相信 html 的问题不像您的 AWeber 示例那样工作,而是它不包含在单个顶级父元素中,这意味着当您解析它以创建
arcode
jQuery 时object 该对象包含您的示例确实有效,恰好包含在顶级
中,因此它没有此问题。
因此,我建议的修复方法就是将输入的 html 自己包装在
元素中,从而使所有内容都可以与
.find()
一起使用。然后简化您的功能,因为以下简短版本可以完成您原始功能所做的所有工作:工作演示: http:// jsfiddle.net/fpK9F/6/ (似乎可以处理您的示例案例)。
您会注意到我使用了 jQuery 属性包含选择器 和
[名称*=“电子邮件”]
。请注意,如果代码中的以下测试是多余的:
在
else
情况下,您无论如何都要设置hiddens
变量中的值,因此您可以替换整个仅包含if
部分的结构 - 正如我在上面的代码中所做的那样。更新:对于
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 checkingarcode.length
or usingarcode.each()
to log the tagnames of the elements inarcode
.)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: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:
In the
else
case you are setting the value that was in thehiddens
variable anyway, so you can replace the whole structure with just the part from theif
- as I did in my code above.UPDATE: For a case-insensitive test of the
name
attribute's value you can do this:(You don't have to use a regular expression, the key point is the
.filter()
function.)Case insensitive demo: http://jsfiddle.net/fpK9F/7/