2

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.

4

4 回答 4

3

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

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

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

但是,现在我们处理的是对象 ( div) 而不是字符串,尝试将对象转换为字符串会产生"[object Object]"。所以修改 append 函数为:

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

查看您的示例更新

于 2010-08-31T18:58:41.023 回答
3

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.

于 2010-08-31T18:37:00.687 回答
1

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

于 2010-08-31T18:38:01.027 回答
0

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

if(someCondition) { $(data).find('#1234').addClass('mynewclass'+someId); }
于 2010-08-31T18:38:43.773 回答