2021-03-28
  • |
  • daafoor
  • |
  • مشاهدات: 1171

مثال عن تابع بسيط بدون بارامترات:

 

<!DOCTYPE html>
<html>
<head>
<script>
function myFunction() {
  document.getElementById("demo").innerHTML = "Hello World!";
}
</script>
</head>

<body>

<p>When you click "Try it", a function will be called.</p>
<p>The function will display a message.</p>

<button onclick="myFunction()">Try it</button>

<p id="demo"></p>

</body>
</html>

مثال عن تابع مع بارامترين:  

 

<!DOCTYPE html>
<html> 
<head> 
<script> 
function myfunction(txt) { 
  document.getElementById("demo").innerHTML = txt
} 
</script> 
</head> 
<body> 

<p>When you click on one of the buttons, a function will be called. The function will display the argument that is passed to it.</p>

<form> 
  <input type="button" onclick="myfunction('Good Morning!')" value="In the Morning"> 
  <input type="button" onclick="myfunction('Good Evening!')" value="In the Evening"> 
</form> 

<p id="demo"></p>

</body> 
</html>

مثال عن تابع يعيد قيمة:  

 

<!DOCTYPE html>
<html>
<body>

<h2>JavaScript Functions</h2>

<p>This example calls a function which performs a calculation and returns the result:</p>

<p id="demo"></p>

<script>
var x = myFunction(4, 3);
document.getElementById("demo").innerHTML = x;

function myFunction(a, b) {
  return a * b;
}
</script>

</body>
</html>

 

مثال عن تابع يحول الحرارة من فهرنهايت إلى سيليسيوس :

 

<!DOCTYPE html>
<html>
<body>

<h2>JavaScript Functions</h2>

<p>This example calls a function to convert from Fahrenheit to Celsius:</p>
<p id="demo"></p>

<script>
function toCelsius(f) {
  return (5/9) * (f-32);
}
document.getElementById("demo").innerHTML = toCelsius(77);
</script>

</body>
</html>

ابحث عن مسائل برمجة جافاسكربت java script | Java script programming بالانجليزي

هل أعجبك المحتوى؟

محتاج مساعدة؟ تواصل مع مدرس اونلاين الان!

التعليقات
لا يوجد تعليقات
لاضافة سؤال او تعليق على المشاركة يتوجب عليك تسجيل الدخول
تسجيل الدخول