يمكن استخدام التوابع واستدعاؤها في أماكن مختلفة
إخفاء نص عند الضغط على زر :
- الحالة الطبيعية لعرض أي عنصر هي Block أي النص مرئي
- يمكن تغيير الخاصية إلى مخفية داخل تابع منفصل
document.getElementById("sh").style.display="none"; - يتم استدعاء التابع عند الحدث onclick
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title> show and hide </title>
</head>
<body>
<button onclick="hidetext();"> hide </button>
<p id="sh"> this text is visible right now </p>
<script>
function hidetext() {
document.getElementById("sh").style.display="none";
}
</script>
</body>
</html>
إظهار نص عند الضغط على زر :
- جعل النص مخفي باستخدام الخاصية
display : none;
- يمكن تغيير الخاصية إلى مرئية داخل تابع منفصل
document.getElementById("sh").style.display="block"; - يتم استدعاء التابع عند الحدث onclick
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title> show and hide </title>
</head>
<style>
#sh {
Display:none;
}
</style>
<body>
<button onclick="showtext();"> hide </button>
<p id="sh"> this text is visible right now </p>
<script>
function showtext() {
document.getElementById("sh").style.display="block";
}
</script>
</body>
</html>
لنقم بإظهار النص بزر وإخفاءه بزر آخر
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title> show and hide </title>
</head>
<style>
#sh {
display:none;
}
</style>
<body>
<button onclick="showtext();"> show </button>
<section id="sh">
<p> this text is visible right now </p>
<button onclick="hidetext();"> hide </button>
</section>
<script>
function hidetext() {
document.getElementById("sh").style.display="none";
}
function showtext() {
document.getElementById("sh").style.display="block";
}
</script>
</body>
</html>
تم إضافة الوسمsection
جرب إضافة صورة إلى الـ section السابق