JavaScript DHTML/jQuery/Paragraph

Материал из Web эксперт
Перейти к: навигация, поиск

Содержание

Access the offset of the second paragraph:

 

<html>
  <head>
    <script type="text/javascript" src="js/jquery-1.3.2.js"></script>
    <script type="text/javascript">
        $(document).ready(function(){
        
            var p = $("p:last");
            var offset = p.offset();
            p.html( "left: " + offset.left + ", top: " + offset.top );
            
        });
    </script>
  </head>
  <body>
    <body>
         <p>Hello</p><p>2nd Paragraph</p>
                 
    </body>
</html>



Add some html to paragraph.

 

<html>
  <head>
    <script type="text/javascript" src="js/jquery-1.3.2.js"></script>
    <script type="text/javascript">
        $(document).ready(function(){
            $("p").click(function () {
              var htmlStr = $(this).html("<span class="red">Hello <b>Again</b></span>");
              
            });
    
        });
    </script>
    <style>
      .selected { color:red; }
      .highlight { background:yellow; }
    </style>    
  </head>
  <body>
    <body>
      <p class="selected highlight">Hello</p>
    </body>
</html>



Add text to the paragraph (notice the bold tag is escaped).

 

<html>
  <head>
    <script type="text/javascript" src="js/jquery-1.3.2.js"></script>
    <script type="text/javascript">
        $(document).ready(function(){
            $("p").text("<b>Some</b> new text.");
        });
    </script>
    <style>
      .selected { color:red; }
      .highlight { background:yellow; }
    </style>    
  </head>
  <body>
    <body>
      <p class="selected highlight">Hello first</p>
      <p class="">Hello</p>
    </body>
</html>



Add two classes to a paragraph

 
<html>
  <head>
    <script type="text/javascript" src="js/jquery-1.3.2.js"></script>
    <script type="text/javascript">
        $(document).ready(function(){
               $("p:last").addClass("selected highlight");
        });
    </script>
    <style>
      .selected { color:red; }
      .highlight { background:yellow; }
    </style>    
  </head>
  <body>
    <body>
      <p>A</p>
      <p>B</p>
      <p>C</p>
    </body>
</html>



Animates all hidden paragraphs to show slowly

 
<html>
  <head>
    <script type="text/javascript" src="js/jquery-1.3.2.js"></script>
    <script type="text/javascript">
        $(document).ready(function(){
            
           $("button").click(function () {
              $("p").show("slow");
           });

        });
    </script>
  </head>
  <body>
    <body>
    
          <button>Show it</button>
          <p style="display:none">Hello</p>
    </body>
</html>



Animates all paragraphs to slide up or down, completing the animation within 600 milliseconds.

 
<html>
  <head>
    <script type="text/javascript" src="js/jquery-1.3.2.js"></script>
    <script type="text/javascript">
        $(document).ready(function(){

            $("button").click(function () {
              $("p").slideToggle("slow");
            });

        });
    </script>
  </head>
  <body>
    <body>
          <button>Toggle</button>
          <p>
        
            This is the paragraph.
          </p>
    </body>
</html>



Append text node to

   
<html>
  <head>
    <script type="text/javascript" src="js/jquery-1.3.2.js"></script>
    <script type="text/javascript">
        $(document).ready(function(){
             $("p").append(document.createTextNode("Hello"));

        });
    </script>
  </head>
  <body>
    <body>
         <p>asdf</p>
         <p>asdf</p>
         <p>asdf</p>
         <p>asdf</p>
  </body>
</html>



Click a paragraph to convert it from html to text.

 

<html>
  <head>
    <script type="text/javascript" src="js/jquery-1.3.2.js"></script>
    <script type="text/javascript">
        $(document).ready(function(){
            $("p").click(function () {
              var htmlStr = $(this).html();
              alert(htmlStr);
            });
    
        });
    </script>
    <style>
      .selected { color:red; }
      .highlight { background:yellow; }
    </style>    
  </head>
  <body>
    <body>
      <p class="selected highlight">Hello</p>
    </body>
</html>



Clones all b elements (and selects the clones) and prepends them to all paragraphs.

 
<html>
  <head>
    <script type="text/javascript" src="js/jquery-1.3.2.js"></script>
    <script type="text/javascript">
        $(document).ready(function(){
              $("b").clone().prependTo("p");
        });
    </script>
  </head>
  <body>
    <body>
            <b>Hello</b><p>, how are you?</p>
                 
    </body>
</html>



Find all the text nodes inside a paragraph and wrap them with a bold tag.

 

<html>
  <head>
    <script type="text/javascript" src="js/jquery-1.3.2.js"></script>
    <script type="text/javascript">
        $(document).ready(function(){
                
              $("p").contents().not("[nodeType=1]").wrap("<b/>");
                    
        });
    </script>
  </head>
  <body>
    <body>
        <p>Hello <a href="http://wbex.ru/">wbex</a>asdf</p>
          
    </body>
</html>



Find the text in the first paragraph (stripping out the html), then set the html of the last paragraph to show it is just text (the red bold is gone).

 

<html>
  <head>
    <script type="text/javascript" src="js/jquery-1.3.2.js"></script>
    <script type="text/javascript">
        $(document).ready(function(){
            var str = $("p:first").text();
            $("p:last").html(str);
        });
    </script>
    <style>
      .selected { color:red; }
      .highlight { background:yellow; }
    </style>    
  </head>
  <body>
    <body>
      <p class="selected highlight">Hello first</p>
      <p class="">Hello</p>
    </body>
</html>



Inserts all paragraphs before an element

 
<html>
  <head>
    <script type="text/javascript" src="js/jquery-1.3.2.js"></script>
    <script type="text/javascript">
        $(document).ready(function(){
          $("p").insertBefore( $("b") );
                
         
        });
    </script>
    
  </head>
  <body>
    <body>
         <b>asdf</b>
         <p>asdf: </p>
         
    </body>
</html>



Locate all the paragraphs after the second child in the body and give them a class.

 

<html>
  <head>
    <script type="text/javascript" src="js/jquery-1.3.2.js"></script>
    <script type="text/javascript">
        $(document).ready(function(){
              $(":nth-child(1)").nextAll("p").css("color","red");
        });
    </script>
  </head>
  <body>
    <body>
              
          <p>p</p>
          <div>div</div>
        
          <p>p</p>
          <p>p</p>
          <div>div</div>
          <p>p</p>
          <div>div</div>
          
    </body>
</html>



Paragraph click event

    
<html>
  <head>
    <script type="text/javascript" src="js/jquery-1.3.2.js"></script>
    <script type="text/javascript">
        $(document).ready(function(){
              $("p").click(function () {
                 $(this).toggleClass("blue");
              });
        });
    </script>
  </head>
   <style>
      .blue { color:blue; }
  </style>
  <body>
    <body>
      <p class=blue>A</p>

  </body>
</html>



Paragraph slide in and out

    
<html>
  <head>
    <script type="text/javascript" src="js/jquery-1.3.2.js"></script>
    <style type="text/css">
        a.test { font-weight: bold; color:red;}
    </style>
    <script type="text/javascript">
 $(document).ready(function(){
    $("input.buttonA").click(function(){ $("div.contentToChange").find("p.firstparagraph:hidden").slideDown("slow"); });
    $("input.buttonB").click(function(){ $("div.contentToChange").find("p.firstparagraph:visible").slideUp("slow"); });

 });
    </script>
  </head>
  <body>
    <input type="button" value="Slide In" class="buttonA" />
    <input type="button" value="Slide Out" class="buttonB" />

    <div style="background:#eee;" class="contentToChange">
        <h2>Header 2</h2>
       
        <p class="firstparagraph">Lorem ipsum <em>dolor</em> sit amet, consectetuer <em>adipiscing</em> elit, sed diam nonummy nibh euismod <em>tincidunt</em> ut laoreet dolore magna aliquam erat <strong>volutpat</strong>. Ut wisi enim ad minim <em>veniam</em>, quis nostrud exerci <strong>tation</strong> ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo consequat.</p>
        
        <p class="secondparagraph">Duis autem vel eum iriure dolor in hendrerit in vulputate velit esse <strong>molestie</strong> consequat, vel illum <strong>dolore</strong> eu feugiat nulla facilisis at vero eros et accumsan et iusto odio dignissim qui blandit praesent luptatum zzril delenit augue duis dolore te feugait nulla facilisi. Lorem ipsum dolor sit amet, consectetuer <strong>adipiscing</strong> elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat.</p>
        
        <p class="thirdparagraph">Ut wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper suscipit lobortis nisl ut aliquip ex ea <em>commodo</em> consequat. Duis autem vel eum iriure dolor in hendrerit in <em>vulputate</em> velit esse molestie consequat, vel illum dolore eu feugiat nulla facilisis at vero eros et accumsan et iusto odio dignissim qui blandit praesent luptatum zzril delenit augue duis dolore te <strong>feugait</strong> nulla facilisi.</p>
        
        <p class="fourthparagraph">Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat. Ut wisi enim ad minim veniam, <strong>quis</strong> nostrud exerci tation ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo consequat.
        
        <p class="fifthparagraph">Lorem ipsum <em>dolor</em> sit amet, consectetuer <em>adipiscing</em> elit, sed diam nonummy nibh euismod <em>tincidunt</em> ut laoreet dolore magna aliquam erat <strong>volutpat</strong>. Ut wisi enim ad minim <em>veniam</em>, quis nostrud exerci <strong>tation</strong> ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo consequat.</p>
        
        <p class="sixthparagraph">Duis autem vel eum iriure dolor in hendrerit in vulputate velit esse <strong>molestie</strong> consequat, vel illum <strong>dolore</strong> eu feugiat nulla facilisis at vero eros et accumsan et iusto odio dignissim qui blandit praesent luptatum zzril delenit augue duis dolore te feugait nulla facilisi. Lorem ipsum dolor sit amet, consectetuer <strong>adipiscing</strong> elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat.</p>
        
    </div>
  </body>
</html>



Remove paragraph with fade and animation

    
<html>
  <head>
    <script type="text/javascript" src="js/jquery-1.3.2.js"></script>
    <style type="text/css">
        a.test { font-weight: bold; color:red;}
    </style>
    <script type="text/javascript">
 $(document).ready(function(){
    $("input.buttonA").click(function(){ $("div.contentToChange").find("p.thirdparagraph").hide("slow"); });

 });
    </script>
  </head>
  <body>
    <input type="button" value="Hide" class="buttonA" />

    <div style="background:#eee;" class="contentToChange">
        <h2>Header 2</h2>
       
        <p class="firstparagraph">Lorem ipsum <em>dolor</em> sit amet, consectetuer <em>adipiscing</em> elit, sed diam nonummy nibh euismod <em>tincidunt</em> ut laoreet dolore magna aliquam erat <strong>volutpat</strong>. Ut wisi enim ad minim <em>veniam</em>, quis nostrud exerci <strong>tation</strong> ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo consequat.</p>
        
        <p class="secondparagraph">Duis autem vel eum iriure dolor in hendrerit in vulputate velit esse <strong>molestie</strong> consequat, vel illum <strong>dolore</strong> eu feugiat nulla facilisis at vero eros et accumsan et iusto odio dignissim qui blandit praesent luptatum zzril delenit augue duis dolore te feugait nulla facilisi. Lorem ipsum dolor sit amet, consectetuer <strong>adipiscing</strong> elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat.</p>
        
        <p class="thirdparagraph">Ut wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper suscipit lobortis nisl ut aliquip ex ea <em>commodo</em> consequat. Duis autem vel eum iriure dolor in hendrerit in <em>vulputate</em> velit esse molestie consequat, vel illum dolore eu feugiat nulla facilisis at vero eros et accumsan et iusto odio dignissim qui blandit praesent luptatum zzril delenit augue duis dolore te <strong>feugait</strong> nulla facilisi.</p>
        
        <p class="fourthparagraph">Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat. Ut wisi enim ad minim veniam, <strong>quis</strong> nostrud exerci tation ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo consequat.
        
        <p class="fifthparagraph">Lorem ipsum <em>dolor</em> sit amet, consectetuer <em>adipiscing</em> elit, sed diam nonummy nibh euismod <em>tincidunt</em> ut laoreet dolore magna aliquam erat <strong>volutpat</strong>. Ut wisi enim ad minim <em>veniam</em>, quis nostrud exerci <strong>tation</strong> ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo consequat.</p>
        
        <p class="sixthparagraph">Duis autem vel eum iriure dolor in hendrerit in vulputate velit esse <strong>molestie</strong> consequat, vel illum <strong>dolore</strong> eu feugiat nulla facilisis at vero eros et accumsan et iusto odio dignissim qui blandit praesent luptatum zzril delenit augue duis dolore te feugait nulla facilisi. Lorem ipsum dolor sit amet, consectetuer <strong>adipiscing</strong> elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat.</p>
        
    </div>
  </body>
</html>



Removes all child nodes (including text nodes) from all paragraphs

 

<html>
  <head>
    <script type="text/javascript" src="js/jquery-1.3.2.js"></script>
    <script type="text/javascript">
        $(document).ready(function(){
                  $("p").click(function () {
                      $(this).empty();
                  });
                
         
        });
    </script>

    
  </head>
  <body>
    <body>
            <p>World</p>
            <div>Replaced!</div>
                 
    </body>
</html>



Removes all paragraphs from the DOM

 

<html>
  <head>
    <script type="text/javascript" src="js/jquery-1.3.2.js"></script>
    <script type="text/javascript">
        $(document).ready(function(){
              $("button").click(function () {
                  $("p").remove();
                });

                
         
        });
    </script>

    
  </head>
  <body>
    <body>
            <p>asdf</p>
            <p>asdf</p>
            <p>asdf</p>
            <button>remove paragraphs</button>
                 
    </body>
</html>



Replace all the paragraphs with bold words.

 

<html>
  <head>
    <script type="text/javascript" src="js/jquery-1.3.2.js"></script>
    <script type="text/javascript">
        $(document).ready(function(){
          $("<b>Paragraph. </b>").replaceAll("span");
                
         
        });
    </script>

    
  </head>
  <body>
    <body>
          <span>Span Text</span>
                 
    </body>
</html>



Replace all the paragraphs with empty div elements.

 


$("p").replaceWith(document.createElement("div"));



Selects all paragraphs and removes those that aren"t of class "selected" or the first one.

 
$("p").filter(".selected, :first")



Selects all paragraphs and removes those without a class "selected".

 

$("p").filter(".selected")



Selects all paragraphs, then slices the selection to include only the first element.

 
<html>
  <head>
    <script type="text/javascript" src="js/jquery-1.3.2.js"></script>
    <script type="text/javascript">
        $(document).ready(function(){
                
           
            $("div").slice(0, 1).wrapInner("<b></b>");
                    
        });
    </script>
  </head>
  <body>
    <body>
          <div>asdf</div>
          <div>asdf</div>
          <div>asdf</div>
          <div>asdf</div>
          
    </body>
</html>



Set HTML for paragraph

    
<html>
  <head>
    <script type="text/javascript" src="js/jquery-1.3.2.js"></script>
    <script type="text/javascript">
        $(document).ready(function(){
               $("p").html("<span class="red">Hello <b>Again</b></span>");
        });
    </script>
  </head>
  <body>
    <body>
         <p>asdf</p>
  </body>
</html>



Set paragraph text value

    
<html>
  <head>
    <script type="text/javascript" src="js/jquery-1.3.2.js"></script>
    <script type="text/javascript">
        $(document).ready(function(){
             $("p").text("<b>Some</b> new text.");
        });
    </script>
  </head>
  <body>
    <body>
         <p>asdf</p>
         <p>asdf</p>
         <p>asdf</p>
         <p>asdf</p>
  </body>
</html>



Starts with all paragraphs and searches for descendant span elements, same as $("p span")

 

<html>
  <head>
    <script type="text/javascript" src="js/jquery-1.3.2.js"></script>
    <script type="text/javascript">
        $(document).ready(function(){
                
         $("p").find("span").css("color","red");
                    
        });
    </script>
  </head>
  <body>
    <body>
        
        <p>bad or <span>good</span>.</p>
          
    </body>
</html>



To change the color of any paragraph to red on mouseover event.

 

<html>
  <head>
    <script type="text/javascript" src="js/jquery-1.3.2.js"></script>
    <script type="text/javascript">
        $(document).ready(function(){
                    $("p").mouseover(function () {
                      $(this).css("color","red");
                    });
        });
    </script>
  </head>
  <body>
    <body>
           <p>asdf</p>
                 
    </body>
</html>



To display the text of all paragraphs in an alert box the first time each of them is clicked:

 

$("p").one("click", function(){
  alert( $(this).text() );
});



Toggles all paragraphs.

 

<html>
  <head>
    <script type="text/javascript" src="js/jquery-1.3.2.js"></script>
    <script type="text/javascript">
        $(document).ready(function(){
                
           $("button").click(function () {
               $("p").toggle();
           });
                
        });
    </script>
  </head>
  <body>
    <body>
      <button>Toggle</button>
      <p>Hello</p>
      <p style="display: none">Good Bye</p>
    </body>
</html>



To hide paragraphs on a page when they are clicked:

 

<html>
  <head>
    <script type="text/javascript" src="js/jquery-1.3.2.js"></script>
    <script type="text/javascript">
        $(document).ready(function(){
            $("p").click(function () { 
              $(this).slideUp(); 
            });
            $("p").hover(function () {
              $(this).addClass("hilite");
            }, function () {
              $(this).removeClass("hilite");
            });
        });
    </script>
  </head>
  <body>
    <body>
          <p>First</p>
          <p>Second</p>
          <p>3</p>
    </body>
</html>



To highlight a clicked word in the paragraph.

 

<html>
  <head>
    <script type="text/javascript" src="js/jquery-1.3.2.js"></script>
    <script type="text/javascript">
        $(document).ready(function(){
        
            var words = $("p:first").text().split(" ");
            var text = words.join("</div> <div>");
            $("p:first").html("<div>" + text + "</div>");
            $("div").click(function () {
              $(this).css("background-color","yellow");
            });
        });
    </script>
  </head>
  <body>
    <body>
          <p>a b c</p>
                 
    </body>
</html>



To set the color of all paragraphs to red and background to blue:

 

<html>
  <head>
    <script type="text/javascript" src="js/jquery-1.3.2.js"></script>
    <script type="text/javascript">
        $(document).ready(function(){
                   $("p").hover(function () {
                      $(this).css({ backgroundColor:"yellow", fontWeight:"bolder" });
                    }, function () {
                      var cssObj = {
                        backgroundColor: "#ddd",
                        color: "rgb(0,40,244)"
                      }
                      $(this).css(cssObj);
                    });
        });
    </script>
  </head>
  <body>
    <body>
           <p>Move the mouse over me.</p>
                 
    </body>
</html>



To unbind all events from all paragraphs

 

$("p").unbind()



Wrap a new div around all of the paragraphs.

 
<html>
  <head>
    <script type="text/javascript" src="js/jquery-1.3.2.js"></script>
    <script type="text/javascript">
        $(document).ready(function(){
          $("p").wrapAll($(".doublediv"));
                
         
        });
    </script>
    <style>
      .doublediv { color:red; }
    </style>
    
  </head>
  <body>
    <body>
          <p>World</p>
          <p>World</p>
          <div class="doublediv">asdf<div>
                 
    </body>
</html>