Translate

حمل تطبيق الموقع

الثلاثاء، 22 أكتوبر 2013

دليلك للـ JavaScript (2)

 

 تكلمنا المرة السابقة عن تعريف  لغة الجافا سكريبت والفرق بينها وبين لغة الجافا وذكرنا بعض خصائصها والـ Function وشرحنا بعض الامثلة  على ذلك وسوف نستعرض فيما يلى أمثلة جديدة  تتعلق بـ  Array Object

· إنشاء مصفوفة (Create an array
 
<!DOCTYPE html>
<html>
<body>
<script>
var i;
var mycars = new Array();
mycars[0] = "Saab";
mycars[1] = "Volvo";
mycars[2] = "BMW";
for (i=0;i<mycars.length;i++)
{
document.write(mycars[i] + "<br>");
}
</script>
</body>
</html>
· إنضمام مصفوفتين (Join two arrays - concat()
<!DOCTYPE html>
<html>
<body>
<p id="demo">Click the button to join three arrays.</p>
<button onclick="myFunction()">Try it</button>
<script>
function myFunction()
{
var hege = ["Cecilie", "Lone"];
var stale = ["Emil", "Tobias", "Linus"];
var kai = ["Robin"];
var children = hege.concat(stale,kai);
var x=document.getElementById("demo");
x.innerHTML=children;
}
</script>
</body>
</html>
·ضم كل عناصر المصفوفة  فى نص واحد (Join all elements of an array into a string - join())
<!DOCTYPE html>
<html>
<body>
<p id="demo">Click the button to join the array elements into a string.</p>
<button onclick="myFunction()">Try it</button>
<script>
function myFunction()
{
var fruits = ["Banana", "Orange", "Apple", "Mango"];
var x=document.getElementById("demo");
x.innerHTML=fruits.join();
}
</script>
</body>
</html>
· إزالة العنصر الأخير من المصفوفة (()Remove the last element of an array - pop)
<!DOCTYPE html>
<html>
<body>
<p id="demo">Click the button to remove the last array element.</p>
<button onclick="myFunction()">Try it</button>
<script>
var fruits = ["Banana", "Orange", "Apple", "Mango"];
function myFunction()
{
fruits.pop();
var x=document.getElementById("demo");
x.innerHTML=fruits;
}
</script>
</body>
</html>
·أضافة عنصر جديد إلى المصفوفة (()Add new elements to the end of an array - push)
<!DOCTYPE html>
<html>
<body>
<p id="demo">Click the button to add a new element to the array.</p>
<button onclick="myFunction()">Try it</button>
<script>
var fruits = ["Banana", "Orange", "Apple", "Mango"];
function myFunction()
{
fruits.push("Kiwi")
var x=document.getElementById("demo");
x.innerHTML=fruits;
}
</script>
</body>
</html>
·عكس ترتيب العناصر في المصفوفة  (()Reverse the order of the elementsin an array - reverse
<!DOCTYPE html>
<html>
<body>
<p id="demo">Click the button to reverse the order of the elements in the array.</p>
<button onclick="myFunction()">Try it</button>
<script>
var fruits = ["Banana", "Orange", "Apple", "Mango"];
function myFunction()
{
fruits.reverse();
var x=document.getElementById("demo");
x.innerHTML=fruits;
}
</script>
</body>
</html>
· إزالة العنصر الأول من المصفوفة (Remove the first element of an array - shift())
<!DOCTYPE html>
<html>
<body>
<p id="demo">Click the button to remove the first element of the array.</p>
<button onclick="myFunction()">Try it</button>
<script>
var fruits = ["Banana", "Orange", "Apple", "Mango"];
function myFunction()
{
fruits.shift();
var x=document.getElementById("demo");
x.innerHTML=fruits;
}
</script>
</body>
</html>
· تحديد عنصر محدد من المصفوفة (Select elements from an array - slice())
<!DOCTYPE html>
<html>
<body>
<p id="demo">Click the button to extract the second and the third elements from the array.</p>
<button onclick="myFunction()">Try it</button>
<script>
function myFunction()
{
var fruits = ["Banana", "Orange", "Lemon", "Apple", "Mango"];
var citrus = fruits.slice(1,3);
var x=document.getElementById("demo");
x.innerHTML=citrus;
}
</script>
</body>
</html>
 
· ترتيب عناصر المصفوفة أبجديا (Sort an array (alphabetically and ascending) - sort())
<!DOCTYPE html>
<html>
<body>
<p id="demo">Click the button to sort the array.</p>
<button onclick="myFunction()">Try it</button>
<script>
function myFunction()
{
var fruits = ["Banana", "Orange", "Apple", "Mango"];
fruits.sort();
var x=document.getElementById("demo");
x.innerHTML=fruits;
}
</script>
</body>
</html>
 
· ترتيب الأرقام تصاعديا (Sort numbers (numerically and ascending) - sort())

<!DOCTYPE html>
<html>
<body>
<p id="demo">Click the button to sort the array.</p>
<button onclick="myFunction()">Try it</button>
<script>
function myFunction()
{
var points = [40,100,1,5,25,10];
points.sort(function(a,b){return a-b});
var x=document.getElementById("demo");
x.innerHTML=points;
}
</script>
</body>
</html>
· ترتيب الأرقام تنازليا (Sort numbers (numerically and descending) - sort())
 
<!DOCTYPE html>
<html>
<body>
<p id="demo">Click the button to sort the array.</p>
<button onclick="myFunction()">Try it</button>
<script>
function myFunction()
{
var points = [40,100,1,5,25,10];
points.sort(function(a,b){return b-a});
var x=document.getElementById("demo");
x.innerHTML=points;
}
</script>
</body>
</html>
· إضافة عنصر إلى المركز 2  فى المصفوفة (Add an element to position 2 in an array - splice())
<!DOCTYPE html>
<html>
<body>
<p id="demo">Click the button to add elements to the array.</p>
<button onclick="myFunction()">Try it</button>
<script>
function myFunction()
{
var fruits = ["Banana", "Orange", "Apple", "Mango"];
fruits.splice(2,0,"Lemon","Kiwi");
var x=document.getElementById("demo");
x.innerHTML=fruits;
}
</script>
</body>
</html>
· تحويل المصفوفة الى نص(Convert an array to a string - toString())
<!DOCTYPE html>
<html>
<body>
<p id="demo">Click the button to convert the array into a String.</p>
<button onclick="myFunction()">Try it</button>
<script>
function myFunction()
{
var fruits = ["Banana", "Orange", "Apple", "Mango"];
fruits.toString();
var x=document.getElementById("demo");
x.innerHTML=fruits;
}
</script>
</body>
</html>
· اضافة عنصر جديدة الى بداية المصفوفة(Add new elements to the beginning of an array - unshift())
<!DOCTYPE html>
<html>
<body>
<p id="demo">Click the button to add elements to the array.</p>
<button onclick="myFunction()">Try it</button>
<script>
function myFunction()
{
var fruits = ["Banana", "Orange", "Apple", "Mango"];
fruits.unshift("Lemon","Pineapple");
var x=document.getElementById("demo");
x.innerHTML=fruits;
}
</script>
<p><b>Note:</b> The unshift() method does not work properly in Internet Explorer 8 and earlier, the values will be inserted, but the return value will be <em>undefined</em>.</p>
</body>
</html>

0 التعليقات:

إرسال تعليق

Twitter Followers, YouTube Subscribers, Website Hits,Facebook Likes


------------------- Free powerful counter for your website. From creators of EasyHits4U