为什么这个 div 不添加 jQuery 类?
我对我的数据库进行 $.get
调用,它返回一些 HTML (data
):
$.get(url, null, function(data) {
它返回的 HTML 是这样的:
<div id="1234" class="myclass">..more html..</div>
在我的回调函数中,我尝试向其中添加一个类(基于我已成功测试的一些条件),如下所示:
if (someCondition) $(data).addClass('mynewclass' + someId);
即使 someCondition
为 true,我的 HTML 也没有 mynewclass[someId] 类添加到其中。有什么理由可以解释为什么我只是愚蠢地忽略了这一点吗?
谢谢... :\
编辑
这是链接到一个可重现的示例。这并不是我本身正在做的事情(我没有使用外部的 var 来获取我的数据,但我正在重现相同的效果)。注意它如何在测试中显示“假”。
I make a $.get
call to my db and it returns some HTML (data
):
$.get(url, null, function(data) {
The HTML it returns is something like this:
<div id="1234" class="myclass">..more html..</div>
In my callback function, I try to add a class to it (based on some conditionals that I have tested are successfully being reached) like so:
if (someCondition) $(data).addClass('mynewclass' + someId);
Even when someCondition
is true, my HTML is not having the mynewclass[someId]
class added to it. Is there any reason why this may be that I'm just stupidly overlooking?
Thanks... :\
EDIT
Here's a link to a reproducible example. It's not EXACTLY what i'm doing per se (i'm not using a var outside to get my data, but i'm reproducing the same effect). notice how it's showing 'false' for the test.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
问题是您正在创建多个 div 对象。您创建的第一个将被丢弃。然后创建一个新的类,它不具有先前添加的类。
保留对首次创建且应用了该类的 div 的引用。然后将相同的 div 添加到页面。
然而,现在我们处理的是一个对象(
div
)而不是一个字符串,并且尝试将对象转换为字符串将产生“[object Object]”
。因此,将追加函数修改为:查看更新的示例。
The problem is you are creating multiple div objects. The first one you are creating is being discarded. Then a new one is created which does not have the class that was previously added.
Hold on to a reference of the div that was first created and to which the class was applied. Then add that same div to the page.
However, now we are dealing with an object (
div
) and not a string, and trying to convert an object to a string will yield"[object Object]"
. So modify the append function to this:See your example updated.
jQuery 就像原始 javascript 一样在 DOM 上工作。而你正在做的,是尝试操作数据(在你的情况下,HTML 是长字符串)。在开始使用 DOM 之前,您至少需要将您请求的 HTML 数据添加到 DOM 中。你的回调应该是这样的。
[ EDIT ]
@Anurag jQuery 确实能够操作字符串数据,但非 ID 事务的情况却并非如此。
您可以看到,
“div”出现在 DOM_STR1 中,与“p”出现在 DOM_STR2 中的方式相同。为什么 jQuery 无法读取包装元素,但可以从字符串中找到子元素?
因此,当数据无论如何都需要存储在 DOM 上时,从字符串中操作它是没有意义的。
jQuery like raw javascript works on DOM. And what you are doing, is trying to manipulate on data( HTML as long string in your case). You need to atleast add HTML data you requested to DOM, before you start doing tricks with it. Your callback should be something like this..
[ EDIT ]
@Anurag Its true that jQuery is able to manipulate strings data, but its not true that its the case with non-ID transactions.
You can see,
'div' is present in DOM_STR1, same way 'p' is present in DOM_STR2. Why jQuery can't read the wrapping element, but finds children from string??
So, when data is anyway needed to be on DOM, it makes no sense to manipulate it from string.
@Jason:这就是我的观点,您需要在 jQuery 对象上使用 .addClass,而不是原始 HTML。或者您需要模式匹配“数据”,将其视为字符串(确实如此)并将您的类注入类属性中。
@Jason: thats my point, you need to use .addClass on a jQuery object, not raw HTML. Or you need to pattern match 'data', treating it as a string (which it is) and inject your class into the class attribute.
“data”变量指的是返回信息的整个集合。像这样的东西应该有效:
The "data" variable refers to the entire collection of returned information. Something like this should work: