为什么这个 div 不添加 jQuery 类?

发布于 2024-09-16 22:22:50 字数 715 浏览 13 评论 0原文

我对我的数据库进行 $.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 技术交流群。

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

发布评论

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

评论(4

笑梦风尘 2024-09-23 22:22:50

问题是您正在创建多个 div 对象。您创建的第一个将被丢弃。然后创建一个新的类,它不具有先前添加的类。

保留对首次创建且应用了该类的 div 的引用。然后将相同的 div 添加到页面。

function(something) {
    var div = $(data).addClass('someClass');
    $('#container').html(div + div.is('.someClass'));
}

然而,现在我们处理的是一个对象(div)而不是一个字符串,并且尝试将对象转换为字符串将产生“[object Object]”。因此,将追加函数修改为:

$('#container').empty();
$('#container').append(div).append(String(div.is('.someClass')));

查看更新的示例

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.

function(something) {
    var div = $(data).addClass('someClass');
    $('#container').html(div + div.is('.someClass'));
}

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:

$('#container').empty();
$('#container').append(div).append(String(div.is('.someClass')));

See your example updated.

扛刀软妹 2024-09-23 22:22:50

jQuery 就像原始 javascript 一样在 DOM 上工作。而你正在做的,是尝试操作数据(在你的情况下,HTML 是长字符串)。在开始使用 DOM 之前,您至少需要将您请求的 HTML 数据添加到 DOM 中。你的回调应该是这样的。

function(data){
  $('#container_to_fit_data').html(data); // Adding to Document
  someId = 1234;
  if (someCondition) 
      $('#'+someId, '#container_to_fit_data').addClass('mynewclass' + someId);
      // or lets see it in simplest form
      // $('#container_to_fit_data').find('#'+someId).addClass('mynewclass' + someId);
}

[ EDIT ]

@Anurag jQuery 确实能够操作字符串数据,但非 ID 事务的情况却并非如此。

您可以看到,

DOM_STR1 = "<div>
              <p>
                 <strong>Strong</strong>
                 <span class='myclass'>Span</span>
              </p>
            </div>"

DOM_STR2 = "<p>
              <strong>Strong</strong>
              <span class='myclass'>Span</span>
            </p>"


$('span.myclass', DOM_STR1) // We find span. Allright.
$('span.myclass', DOM_STR2) // We find span. Cool.

$('p', DOM_STR1) // We find p too. But..
$('p', DOM_STR2) // Empty. Never return p, Why??
$('div', DOM_STR1) // Empty Again. Why??

“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..

function(data){
  $('#container_to_fit_data').html(data); // Adding to Document
  someId = 1234;
  if (someCondition) 
      $('#'+someId, '#container_to_fit_data').addClass('mynewclass' + someId);
      // or lets see it in simplest form
      // $('#container_to_fit_data').find('#'+someId).addClass('mynewclass' + someId);
}

[ 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,

DOM_STR1 = "<div>
              <p>
                 <strong>Strong</strong>
                 <span class='myclass'>Span</span>
              </p>
            </div>"

DOM_STR2 = "<p>
              <strong>Strong</strong>
              <span class='myclass'>Span</span>
            </p>"


$('span.myclass', DOM_STR1) // We find span. Allright.
$('span.myclass', DOM_STR2) // We find span. Cool.

$('p', DOM_STR1) // We find p too. But..
$('p', DOM_STR2) // Empty. Never return p, Why??
$('div', DOM_STR1) // Empty Again. Why??

'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.

残月升风 2024-09-23 22:22:50

@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.

凉宸 2024-09-23 22:22:50

“data”变量指的是返回信息的整个集合。像这样的东西应该有效:

if(someCondition) { $(data).find('#1234').addClass('mynewclass'+someId); }

The "data" variable refers to the entire collection of returned information. Something like this should work:

if(someCondition) { $(data).find('#1234').addClass('mynewclass'+someId); }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文