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

javascript - Read each line of an element in jQuery - Stack Overflow

programmeradmin1浏览0评论

Suppose I have this html markup:

<div id="wrapper">
 <pre class="highlight">
    $(function(){
    // hide all links except for the first
    $('ul.child:not(:first)').hide();
    $("a.slide:first").css("background-color","#FF9900");

    $('ul.parent a.slide').click(function(){
        $('ul.parent a.slide').css("background-color","#3399FF");

        $('ul.parent a.slide').mouseout(function(){
            $(this).css("background-color","#3399FF");
        });

        $('ul.parent a.slide').mouseover(function(){
            $(this).css("background-color","#66CC66");
        });
    });
    });
  </pre>
</div>

What is the easiest way to read each line of code present in the div. How do I extract each line of code from that div or loop over each line styling in any way I want.

Edit:

I have added the pre tag after the wrapper class, please consider that also.

Suppose I have this html markup:

<div id="wrapper">
 <pre class="highlight">
    $(function(){
    // hide all links except for the first
    $('ul.child:not(:first)').hide();
    $("a.slide:first").css("background-color","#FF9900");

    $('ul.parent a.slide').click(function(){
        $('ul.parent a.slide').css("background-color","#3399FF");

        $('ul.parent a.slide').mouseout(function(){
            $(this).css("background-color","#3399FF");
        });

        $('ul.parent a.slide').mouseover(function(){
            $(this).css("background-color","#66CC66");
        });
    });
    });
  </pre>
</div>

What is the easiest way to read each line of code present in the div. How do I extract each line of code from that div or loop over each line styling in any way I want.

Edit:

I have added the pre tag after the wrapper class, please consider that also.

Share Improve this question edited Apr 11, 2018 at 22:15 halfer 20.4k19 gold badges108 silver badges201 bronze badges asked Apr 7, 2010 at 14:01 SarfrazSarfraz 383k82 gold badges559 silver badges612 bronze badges 2
  • 2 I assume you have white-space: pre in the CSS for #wrapper or something? – T.J. Crowder Commented Apr 7, 2010 at 14:05
  • 1 That is evil in it's purest form. – googletorp Commented Apr 7, 2010 at 14:11
Add a comment  | 

2 Answers 2

Reset to default 15

Do you mean something like this:

var lines = $("#wrapper pre").text().split("\n");
$.each(lines, function(n, elem) {
            console.log(elem);
          });

Since this is text (and not html) you should treat it as such. The only way to do it is to read it and then use string parsing routines.

Mostly just need straight JavaScript:

var text;

text = $('#wrapper pre').text(); // Or .html, doesn't really matter with the input you showed
text = text.replace("\r\n", "\n"); // Just in case
text = text.split("\n");
// text is now an array of lines

Some browsers will trim the first line break, some won't, so there are a couple of edge cases (no pun). But that's basically it.

发布评论

评论列表(0)

  1. 暂无评论