ช่วยหน่อยค่ะ ทำยังไงให้ List หายไปก่อน แล้วให้เเสดงอีกทีตอน search
ถาม-ตอบ แนะนำไอเดียว โค้ดตัวอย่าง แนวทาง วิธีแก้ปัญหา ช่วยหน่อยค่ะ ทำยังไงให้ List หายไปก่อน แล้วให้เเสดงอีกทีตอน search
ช่วยหน่อยค่ะ ทำยังไงให้ List หายไปก่อน แล้วให้เเสดงอีกทีตอน search
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | <!doctype html> < html > < head > < meta charset = "UTF-8" > < title >Restaurant</ title > < link rel = "stylesheet" type = "text/css" href = "test.css" > < script type = "text/javascript" src = "script.js" ></ script > </ head > < body > < input type = "text" id = "myInput" onkeyup = "myFunction()" placeholder = "Ange restaurangnamn" > < ul id = "myUL" > < li >< a href = "#" >Hot Wok &Pizza Take Away</ a ></ li > < li >< a href = "#" >Piazza Teatro</ a ></ li > < li >< a href = "#" >Restaurang PM och Vänner</ a ></ li > < li >< a href = "#" >Evedals värdhus </ a ></ li > < li >< a href = "#" >Båtsmannen</ a ></ li > < li >< a href = "#" >Miso Japansk Restaurang</ a ></ li > < li >< a href = "#" >Stars N Stripes</ a ></ li > < li >< a href = "#" >La Castellina</ a ></ li > </ ul > </ body > </ html > |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 | @charset "UTF-8" ; /* CSS Document */ #myInput { background-image : url ( '/css/searchicon.png' ); /* Add a search icon to input */ background-position : 10px 12px ; /* Position the search icon */ background-repeat : no-repeat ; /* Do not repeat the icon image */ width : 100% ; /* Full-width */ font-size : 16px ; /* Increase font-size */ padding : 12px 20px 12px 40px ; /* Add some padding */ border : 1px solid #ddd ; /* Add a grey border */ margin-bottom : 12px ; /* Add some space below the input */ } #myUL { /* Remove default list styling */ list-style-type : none ; padding : 0 ; margin : 0 ; } #myUL li a { border : 1px solid #ddd ; /* Add a border to all links */ margin-top : -1px ; /* Prevent double borders */ background-color : #f6f6f6 ; /* Grey background color */ padding : 12px ; /* Add some padding */ text-decoration : none ; /* Remove default text underline */ font-size : 18px ; /* Increase the font-size */ color : black ; /* Add a black text color */ display : block ; /* Make it into a block element to fill the whole list */ } #myUL li a.header { background-color : #e2e2e2 ; /* Add a darker background color for headers */ cursor : default ; /* Change cursor style */ } #myUL li a:hover:not(.header) { background-color : #eee ; /* Add a hover effect to all links, except for headers */ } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | // JavaScript Document function myFunction() { // Declare variables var input, filter, ul, li, i; input = document.getElementById( 'myInput' ); filter = input.value.toUpperCase(); ul = document.getElementById( "myUL" ); li = ul.getElementsByTagName( 'li' ); // Loop through all list items, and hide those who don't match the search query for (i = 0; i < li.length; i++) { a = li[i].getElementsByTagName( "a" )[0]; if (a.innerHTML.toUpperCase().indexOf(filter) > -1) { li[i].style.display = "" ; } else { li[i].style.display = "none" } } } |

คำแนะนำ และการใช้งาน
สมาชิก กรุณา ล็อกอินเข้าระบบ เพื่อตั้งคำถามใหม่ หรือ ตอบคำถาม สมาชิกใหม่ สมัครสมาชิกได้ที่ สมัครสมาชิก
- ถาม-ตอบ กรุณา ล็อกอินเข้าระบบ
ความคิดเห็นที่
1
ลองประมาณนี้ดู
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | function myFunction() { // Declare variables var input, filter, ul, li, i; input = document.getElementById( 'myInput' ); filter = input.value.toUpperCase(); ul = document.getElementById( "myUL" ); li = ul.getElementsByTagName( 'li' ); if (filter.length>0){ ul.style.display = "" ; // Loop through all list items, and hide those who don't match the search query for (i = 0; i < li.length; i++) { a = li[i].getElementsByTagName( "a" )[0]; if (a.innerHTML.toUpperCase().indexOf(filter) > -1) { li[i].style.display = "" ; } else { li[i].style.display = "none" } } } else { ul.style.display = "none" } } |

ขอบคุณทุกการสนับสนุน
![]()