ยินดีต้อนรับคุณ, บุคคลทั่วไป กรุณา เข้าสู่ระบบ หรือ ลงทะเบียน

เข้าสู่ระบบด้วยชื่อผู้ใช้ รหัสผ่าน และระยะเวลาในเซสชั่น

ThaiSEOBoard.comพัฒนาเว็บไซต์Programmingขอวิธีแก้หน่อยครับ ปัญหาเกี่ยวกับ Jquery
หน้า: [1]   ลงล่าง
พิมพ์
ผู้เขียน หัวข้อ: ขอวิธีแก้หน่อยครับ ปัญหาเกี่ยวกับ Jquery  (อ่าน 764 ครั้ง)
0 สมาชิก และ 1 บุคคลทั่วไป กำลังดูหัวข้อนี้
suppend
Newbie
*

พลังน้ำใจ: 0
ออฟไลน์ ออฟไลน์

กระทู้: 11



ดูรายละเอียด
« เมื่อ: 05 กุมภาพันธ์ 2018, 09:48:39 »

โค๊ด:
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
    $("button").click(function(){
       var num=$(this).attr('rel');
       $('p').each(function(){
           $(this).hide();
       });
       $('#p' num).show();
    });
});
</script>
</head>
<body>

<p id="p1">Show Detaill 1</p>
<button rel="1">Click #1</button><br>
<p id="p2">Show Detaill 2</p>
<button rel="2">Click #2</button><br>
<p id="p3">Show Detaill 3</p>
<button rel="3">Click #3</button><br>
<p id="p4">Show Detaill 1</p>
<button rel="4">Click #4</button>

</body>
</html>

จาก code นี้ต้องการให้เปิดขึ้นมาแล้ว Hide ทั้งหมดก่อนครับ และเปลี่ยนจาก button เป็นลิงค์ข้อความ ต้องเขียนแก้ยังไงครับ

ขอบพระคุณครับ
 wanwan017 wanwan017
บันทึกการเข้า
deadclosed
หัวหน้าแก๊งเสียว
*

พลังน้ำใจ: 133
ออฟไลน์ ออฟไลน์

กระทู้: 1,553



ดูรายละเอียด เว็บไซต์
« ตอบ #1 เมื่อ: 05 กุมภาพันธ์ 2018, 10:33:07 »

โค๊ด:
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<style>
button {
-moz-user-select: text;
background: none;
border: none;
color: blue;
cursor: pointer;
font-size: 1em;
margin: 0;
padding: 0;
text-align: left;
}
 
button.link:hover span {
text-decoration: underline;
}
</style>

<script>
$(document).ready(function(){
$('p').hide();
    $("button").click(function(){
       var num=$(this).attr('rel');
       $( "#p" num ).toggle( "slow", function() {
        // Animation complete.
      });
    });
});
</script>

</head>
<body>

<p id="p1">Show Detaill 1</p>
<button rel="1">Click #1</button><br>
<p id="p2">Show Detaill 2</p>
<button rel="2">Click #2</button><br>
<p id="p3">Show Detaill 3</p>
<button rel="3">Click #3</button><br>
<p id="p4">Show Detaill 1</p>
<button rel="4">Click #4</button>

</body>
</html>

เดาเอาว่าต้องการแบบนี้  wanwan001
บันทึกการเข้า
ThaNaButS
Verified Seller
หัวหน้าแก๊งเสียว
*

พลังน้ำใจ: 67
ออฟไลน์ ออฟไลน์

กระทู้: 1,197



ดูรายละเอียด
« ตอบ #2 เมื่อ: 05 กุมภาพันธ์ 2018, 12:35:48 »

แบบนี้ป่าว
1. ซ่อนส่วนที่ต้องการก่อนด้วย css
โค๊ด:
p{display: none;}

2.ลองดูตรงโค๊ดตรงนี้ $('#p'มันมีบวกด้วยnum).show();  << อ่อ Thaiseoboard ไม่แสดงบวก 

โค๊ด:
<!DOCTYPE html>
<html>
<head>
<style>
p{display: none;}
</style>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
    $("button").click(function(){
       var num=$(this).attr('rel');
       $('p').each(function(){
         $(this).hide();
       });
       $('#p'num).show(); //อย่าลืมใส่บวก
    });
});
</script>
</head>
<body>

<p id="p1">Show Detaill 1</p>
<button rel="1">Click #1</button><br>
<p id="p2">Show Detaill 2</p>
<button rel="2">Click #2</button><br>
<p id="p3">Show Detaill 3</p>
<button rel="3">Click #3</button><br>
<p id="p4">Show Detaill 1</p>
<button rel="4">Click #4</button>

</body>
</html>
« แก้ไขครั้งสุดท้าย: 05 กุมภาพันธ์ 2018, 12:37:58 โดย ThaNaButS » บันทึกการเข้า
หน้า: [1]   ขึ้นบน
พิมพ์