最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - getElementsByTagName vs jQuery - Stack Overflow

programmeradmin0浏览0评论

What would the version of this be in jquery?

document.getElementsByTagName("pre")[0].innerHTML;

I need help converting this in order to fit into my $.get request:

$.get( 
    link, 
    function(data) {
        var res = $(data).find("pre").html();
        console.log(res);
    }
); 

What would the version of this be in jquery?

document.getElementsByTagName("pre")[0].innerHTML;

I need help converting this in order to fit into my $.get request:

$.get( 
    link, 
    function(data) {
        var res = $(data).find("pre").html();
        console.log(res);
    }
); 
Share Improve this question asked Jan 15, 2014 at 0:14 JordanJordan 4952 gold badges10 silver badges22 bronze badges 2
  • 2 vanilla-js. – Niet the Dark Absol Commented Jan 15, 2014 at 0:15
  • What are you getting in the console log ? – adeneo Commented Jan 15, 2014 at 0:17
Add a ment  | 

4 Answers 4

Reset to default 5

The exact [JQuery equivalent] would be $('pre').eq(0).html(). The sortof-ish mix with non-JQuery would be $('pre')[0].innerHTML

How's it work?

$('pre') returns an Object with all elements with a tag name of pre

.eq(0) gets the first element in the array.

DEMO


Since you're getting the first item, $('pre').first().html() also works.

DEMO


Another thing that works would be just $('pre').html() (Credit to RobG)

DEMO


Please note that JQuery's html method is not identical to a browser's innerHTML property but it's the JQuery equivalent (Credit to RobG).


Here it is in jQuery:

$('pre').first().html()

If you only need one element, give the element and ID and pull it by ID

document.getElementById("ID")

Simply specify the element name in the selector:

$("pre").html()

It's not necessary to explicitly select the first element. From the API docs:

If the selector expression matches more than one element, only the first match will have its HTML content returned

发布评论

评论列表(0)

  1. 暂无评论