русский  עברית
tadam logo
דוגמאות jQuery
מצאתם שגיאה?
סמנו אותה עם העכבר ותלחצו
Ctrl + Enter
בדיקת כתיב Xhtml.co.il
Orphus system
תוכן:

css( name )

מחזירה: מחרוזת
הוספה בגרסה: 1.0
מחזיר מאפיין סגנון של אלמנט ראשון בהתאמה.
קיצור מאפיינים ( למשל, margin, background, border ) לא נתמך. למשל, אם צריך לקבל ערך margin, תכתבו: $(elem).css('marginTop') ו- $(elem).css('marginRight'), וכך הלאה.

ארגומנטים

שם של מאפיין שאליו יש לקבל גישה.

דוגמאות

דוגמה 1

מקבל גישה לצבע רקע של אלמנט div, שעליו משתמש לחץ.
    $("div").click(function () {
      var color = $(this).css("background-color");
      $("#result").html("That div is <span style='color:" +
                         color + ";'>" + color + "</span>.");
    });
תוצאת הפעלה קוד לעיל:
קוד מלא:
<!DOCTYPE html>
<html>
<head>
  <script src="http://code.jquery.com/jquery-1.5.js"></script>
  <script>
  $(document).ready(function(){
    $("div").click(function () {
      var color = $(this).css("background-color");
      $("#result").html("That div is <span style='color:" +
                         color + ";'>" + color + "</span>.");
    });
  });
  </script>
  <style>
  div { width:60px; height:60px; margin:5px; float:left; }
  </style>
</head>
<body>
  <span id="result"> </span>
  <div style="background-color:blue;"></div>
  <div style="background-color:rgb(15,99,30);"></div>
  <div style="background-color:#123456;"></div>
  <div style="background-color:#f11;"></div>
</body>
</html>

css( properties )

מחזירה: jQuery
הוספה בגרסה: 1.0
מגדיר מאפיינים של סגנון css על כל אלמנטים בהתאמה, משתמש באובייקט, שמכיל זוגות מפתח/ערך.

זאת דרך הטובה להגדיר כמות גדולה של מאפיינים של סגנון עבור כל אלמנט בהתאמה.

שימו לב, שאם מפתח מכיל מקף, למשל,   "background-color,"  , אז יש להכניס אותו בגרשיים, או לרשום בצורה הבאה   ( camel case )   כמו: backgroundColor. מכיוון ש   "float"   ו-   "class"   הן מילים שמורות ב- JavaScript, מומלץ לעטוף אותן עם גרשיים. jQuery מנרמל מאפיין   "opacity"   ב-Internet Explorer.

ארגומנטים

properties
זוגות מפתח/ערך להגדרת מאפיינים של סגנון.

דוגמאות

דוגמה 1

מגדיר צבע של גופן של כל הפסקאות וצבע רקע:
    $("p").hover(function () {
      $(this).css({'background-color' : 'yellow', 'font-weight' : 'bolder'});
    }, function () {
      var cssObj = {
        'background-color' : '#ddd',
        'font-weight' : '',
        'color' : 'rgb(0,40,244)'
      }
      $(this).css(cssObj);
    });
תוצאת הפעלה קוד לעיל:
קוד מלא:
<!DOCTYPE html>
<html>
<head>
  <script src="http://code.jquery.com/jquery-1.5.js"></script>
  <script>
  $(document).ready(function(){
    $("p").hover(function () {
      $(this).css({'background-color' : 'yellow', 'font-weight' : 'bolder'});
    }, function () {
      var cssObj = {
        'background-color' : '#ddd',
        'font-weight' : '',
        'color' : 'rgb(0,40,244)'
      }
      $(this).css(cssObj);
    });
  });
  </script>
  <style>
  p { color:green; }
  </style>
</head>
<body>
  <p>
    Move the mouse over a paragraph.
  </p>
  <p>
    Like this one or the one above.
  </p>
</body>
</html>

דוגמה 2

אם שם מאפיין מכיל "-", יש לשים אותו בין הגרשיים:
    $("p").hover(function () {
      $(this).css({ "background-color":"yellow", "font-weight":"bolder" });
    }, function () {
      var cssObj = {
        "background-color": "#ddd",
        "font-weight": "",
        color: "rgb(0,40,244)"
      }
      $(this).css(cssObj);
    });
תוצאת הפעלה קוד לעיל:
קוד מלא:
<!DOCTYPE html>
<html>
<head>
  <script src="http://code.jquery.com/jquery-1.5.js"></script>
  <script>
  $(document).ready(function(){
    $("p").hover(function () {
      $(this).css({ "background-color":"yellow", "font-weight":"bolder" });
    }, function () {
      var cssObj = {
        "background-color": "#ddd",
        "font-weight": "",
        color: "rgb(0,40,244)"
      }
      $(this).css(cssObj);
    });
  });
  </script>
  <style>
  p { color:green; }
  </style>
</head>
<body>
  <p>
    Move the mouse over a paragraph.
  </p>
  <p>
    Like this one or the one above.
  </p>
</body>
</html>

css( name, value )

מחזירה: jQuery
הוספה בגרסה: 1.0
מגדיר ערך של מאפיין סגנון אחד עבור כל אלמנטים בהתאמה.
אם צוין מספר, אז הוא אוטומטי יהומר לערך בפקסלים, עם חריגים הבאים: z-index, font-weight, opacity, zoom ו- line-height. כדי למחוק מאפיין מאלמנט, תשתמשו בערך עם מחרוזת רייקה.

ארגומנטים

שם של המאפיין להגדרה.
שם של המאפיין להגדרה. במקרה של Callback, ההקשר הוא לשינוי אלמנט DOM.

דוגמאות

דוגמה 1

מחליף צבע של כל פסקה לאדום באירוע mouseover.
    $("p").mouseover(function () {
      $(this).css("color","red");
    });
תוצאת הפעלה קוד לעיל:
קוד מלא:
<!DOCTYPE html>
<html>
<head>
  <script src="http://code.jquery.com/jquery-1.5.js"></script>
  <script>
  $(document).ready(function(){
    $("p").mouseover(function () {
      $(this).css("color","red");
    });
  });
  </script>
  <style>
  p { color:blue; width:200px; font-size:14px; }
  </style>
</head>
<body>
  <p>
    Just roll the mouse over me.
  </p>
  <p>
    Or me to see a color change.
  </p>
</body>
</html>

דוגמה 2

מסמן מילה שלחצתם עליה בפסקה.
    var words = $("p:first").text().split(" ");
    var text = words.join("</span> <span>");
    $("p:first").html("<span>" + text + "</span>");
    $("span").click(function () {
      $(this).css("background-color","yellow");
    });
תוצאת הפעלה קוד לעיל:
קוד מלא:
<!DOCTYPE html>
<html>
<head>
  <script src="http://code.jquery.com/jquery-1.5.js"></script>
  <script>
  $(document).ready(function(){
    var words = $("p:first").text().split(" ");
    var text = words.join("</span> <span>");
    $("p:first").html("<span>" + text + "</span>");
    $("span").click(function () {
      $(this).css("background-color","yellow");
    });
  });
  </script>
  <style>
  p { color:blue; font-weight:bold; cursor:pointer; }
  </style>
</head>
<body>
  <p>
    Once upon a time there was a man
    who lived in a pizza parlor. This
    man just loved pizza and ate it all 
    the time.  He went on to be the
    happiest man in the world.  The end.
  </p>
</body>
</html>
האם מידע זה היה מועיל?
   

תגובות