Use Jquery Selectors on $.AJAX loaded HTML?

前端 未结 9 863
没有蜡笔的小新
没有蜡笔的小新 2020-11-27 11:30

I have

$.ajax({
  url: identity,
  success: function(data) { ProcessIdentityServer(data) }
});

When \'data\' is returned, is there a way to

9条回答
  •  庸人自扰
    2020-11-27 12:10

    This is the same as the accepted answer but with some extra explanation.

    You may use the jQuery context parameter Link to docs

    I can't really explain better than the documentation.

    Selector Context

    By default, selectors perform their searches within the DOM starting at the document root. However, an alternate context can be given for the search by using the optional second parameter to the $() function

    The context parameter has been around since jQuery v1.0

    Therefore a solution to the OP's example to "get all the href values of any LINK tags contained in the HTML held in 'data' without adding it to the DOM first" would be:

    success: function(data){
        $("a", data).each(function(){
            console.log( $(this).attr("href") );
        });
    }
    

提交回复
热议问题