jQuery - selectors on a html string

后端 未结 4 764
野的像风
野的像风 2020-12-14 15:36

I am going to get a element from a html-string with jQuery, but I always get an undefined in my console. My String is:

asd         


        
4条回答
  •  猫巷女王i
    2020-12-14 16:01

    First, use jQuery.parseHTML to parse the HTML into an array of elements; then you’ll be able to convert it to a jQuery collection and use filter to restrict the collection to elements matching a selector.

    var html =
        'asd' +
        'fgh' +
        'jkl';
    
    var text = $($.parseHTML(html)).filter('.test').text();
    
    console.log(text);

提交回复
热议问题