JavaScript DHTML/jQuery/attr — различия между версиями

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

Текущая версия на 07:23, 26 мая 2010

Add attribute and clear attribute

 
<html>
  <head>
    <script type="text/javascript" src="js/jquery-1.3.2.js"></script>
    <script type="text/javascript">
$(document).ready(
  function() {
    $("div").hover(
      function() {
        $(this).attr("id", "justAdd");
        $(this).text("This element\"s ID is: " + $(this).attr("id"));
      },
      function() {
        $(this).attr("id", "");
        $(this).text("This element\"s ID has been removed.");
      }
    );
  }
);
$("a").removeAttr("title");
    </script>
    <style type="text/css">
div {
    width: 350px;
    padding: 10px;
    border: 1px solid rgb(200, 200, 200);
    background: #93cdf9;
    margin: 5px;
}
div#justAdd {
    background: #6faddd;
}
    </style>
  </head>
  <body>
     <div>Mouse over to change this element"s id.</div>
  </body>
</html>



attr(key, fn)

  
<html>
  <head>
    <script type="text/javascript" src="js/jquery-1.3.2.js"></script>
    <script type="text/javascript">
        $(document).ready(function(){
             $("div").attr("id", function (arr) {
                  return "div-id" + arr;
                 })
                .each(function () {
                  $("span", this).html("(ID = "<b>" + this.id + "</b>")");
             });
        });
    </script>
  </head>
  <body>
    <body>
      <div>A<span></span></div>
      <div>B<span></span></div>
      <div>C<span></span></div>
    </body>
</html>



attr(key, value) set a single property to a value, on all matched elements.

  
<html>
  <head>
    <script type="text/javascript" src="js/jquery-1.3.2.js"></script>
    <script type="text/javascript">
        $(document).ready(function(){
            $("button:gt(1)").attr("disabled","disabled");
      
        });
    </script>
  </head>
  <body>
    <body>
      <button>0th Button</button>
      <button>1st Button</button>
      <button>2nd Button</button>
    </body>
</html>



attr(name): access a property on the first matched element.

  
<html>
  <head>
    <script type="text/javascript" src="js/jquery-1.3.2.js"></script>
    <script type="text/javascript">
        $(document).ready(function(){
            var title = $("em").attr("title");
            $("div").text(title);
      
        });
    </script>
  </head>
  <body>
    <body>
      
      <em title="title value">
      <div></div>
    </body>
</html>



Disable all form input controls

  
<html>
  <head>
    <script type="text/javascript" src="js/jquery-1.3.2.js"></script>
    <script type="text/javascript">
        $(document).ready(function(){
              $("input").attr("disabled", "disabled");
        });
    </script>
  </head>
  <body>
    <body>
    <div>
        <input type="checkbox" name="a" />
    
        <span>A</span>
      </div>
      <div>
        <input type="checkbox" name="b" />
        <span>B</span>
      </div>
      <div>
        <input type="checkbox" name="c" checked="checked" />
    
        <span>C</span>
    </div>
    </body>
</html>



Disable input control

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

  });
    </script>
  </head>
  <body>
   <form>
    <input type="checkbox" name="a" />
    <span>A</span>
    <input type="checkbox" name="b" />
    <span>B</span>
    <input type="checkbox" name="c" checked="checked" />
    <span>C</span>
  </form>

  </body>
</html>



Finds the title attribute of the first in the page.

  

<html>
  <head>
    <script type="text/javascript" src="js/jquery-1.3.2.js"></script>
    <script type="text/javascript">
        $(document).ready(function(){
            var title = $("em").attr("title");
            $("div").text(title);
      
        });
    </script>
  </head>
  <body>
    <body>
      
      <em title="title value">
      <div></div>
    </body>
</html>



Get attribute from tag

     
<html>
  <head>
    <script type="text/javascript" src="js/jquery-1.3.2.js"></script>
    <script type="text/javascript">
        $(document).ready(function(){
            var title = $("em").attr("title");
            $("div").text(title);
        });
    </script>
  </head>
  <body>
    <em title="title value">em</em>
    <div></div>
  </body>
</html>



Get input control type

     
<html>
  <head>
    <script type="text/javascript" src="js/jquery-1.3.2.js"></script>
    <script type="text/javascript">
    $(document).ready(function(){
        $("#exampleTable").find("td").each(function(i, el) {
        var inputEl = $(el).children().get(0);
        $(el).before("<td>Added " + $(inputEl).attr("type") + "</td>");
    })
        
    });
    </script>
  </head>
  <body>
     <form>
     <table id="exampleTable">
     <tr>
        <th>
            Type
        </th>
        <th>
            Element
        </th>
     </tr>
     <tr>
        <td>
            <input type="button" value="Input Button"/>
        </td>
     </tr>
     <tr>
        <td>
            <input type="checkbox" />
        </td>
    </tr>
    </table>
    </form>

  </body>
</html>



Set attribute with the returned function value

 

<html>
  <head>
    <script type="text/javascript" src="js/jquery-1.3.2.js"></script>
    <script type="text/javascript">
$(document).ready(
  function() {
    $("li").attr(
      "id",
      function() {
        return "tmp" + $(this).text();
      }
    );
  }
);
    </script>
    <style type="text/css">
ul {
    list-style: none;
    padding: 0;
    margin: 0;
}
ul li {
    margin: 3px;
    padding: 3px;
}
li#tmpA {
    background: #d7b05b;
}
li#tmpB {
    background: #d3988a;
}
li#tmpC {
    background: #8ad3a6;
}
li#tmpD {
    background: #8aa9d3;
}
    </style>
  </head>
  <body>
     <ul>
        <li>A</li>
        <li>B</li>
        <li>C</li>
        <li>D</li>
     </ul>
  </body>
</html>



Set Image URL title alt

     
<html>
  <head>
    <script type="text/javascript" src="js/jquery-1.3.2.js"></script>
    <script type="text/javascript">
        $(document).ready(function(){
             $("img").attr({ 
                 src: "http://wbex.ru/style/logo.png",
                 title: "wbex.ru",
                 alt: "Logo"
             });
             $("div").text($("img").attr("alt"));
        });
    </script>
  </head>
  <body>
    <body>
      <img />
      <div></div>
  </body>
</html>



Sets src attribute from title attribute on the image.

  
<html>
  <head>
    <script type="text/javascript" src="js/jquery-1.3.2.js"></script>
    <script type="text/javascript">
        $(document).ready(function(){
                $("img").attr("src", function() { 
                  return "/images/" + this.title; 
                });
        });
    </script>
  </head>
  <body>
    <body>
      <img title="a.gif"/>
    </body>
</html>