简体   繁体   中英

Return and Remove Element from HTML String

I'm passing HTML back to an ajax call. In the HTML I want to strip out an element and store its HTML in a variable. I've tried:

request.success(function(results){
    var timer = $(results).find('#timer').html();   // returns undefined

    var timer = $('#timer', results);       // returns element but not content
    results = $(results).remove('#timer');  // doesn't remove element
    console.log(timer);
});

Do I need to run a regex search on the element itself? That could get extensive.

If I console log my HTML (results) I end up with:

e.fn.init[11]
    0: div#timer
    1: text
    2: comment
    3: text
    4: div.pagination.top
    5: text
    6: comment
    7: text
    8: div#postContainer
    9: text
    10: comment
    length: 11
    __proto__: Object[0]

Edit

results appear to be DOM elements, not jquery objects, index ed starting at 0 . Based on what console returned, should be able to access any of the results items based on their respective index, ieg, $(results).get(4) should return div.pagination.top

get #timer html

   var timer = $(results).get(0);
   timerContents = timer.innerHTML;

remove #timer from results

   var result = $.makeArray( results ); 
   result.splice(0, 1);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM