简体   繁体   English

如何使用 JavaScript 删除填充?

[英]How do I remove padding with JavaScript?

 var urlmenu = document.getElementById('category'); urlmenu.onchange = function() { document.location.href = this.value; }; function search_designs() { let input = document.getElementById('searchbar').value input=input.toLowerCase(); let x = document.getElementsByClassName('summary-title-link'); let z = document.getElementsByClassName('summary-item'); for (i = 0; i < x.length; i++) { if (!x[i].innerHTML.toLowerCase().includes(input)) { x[i].style.display="none"; z[i].style.display="none"; } else { x[i].style.display="list-item"; z[i].style.display="block"; } } }
 #searchbar{ text-align: left; position: center; } .search_designs{ display: list-item; }
 <input id="searchbar" type="text" name="search" onkeyup="search_designs()" placeholder="Search.."> <select id="category" name="category"> <option value="https://www.keslyns.com/all-designs-details">All Designs</option> <option value="https://www.keslyns.com/alphabet-quote-designs">Alphabet/Wording</option> <option value="https://www.keslyns.com/box-designs">Boxes</option> <option value="https://www.keslyns.com/floral-designs">Floral</option> <option value="https://www.keslyns.com/heart-designs">Hearts</option> <option value="https://www.keslyns.com/holidayseasonal-designs">Holiday/Seasonal</option> <option value="https://www.keslyns.com/mulitple-motif-designs">Multiple Motif</option> <option value="https://www.keslyns.com/new-designs">New Designs</option> <option value="https://www.keslyns.com/ornament-designs">Ornaments</option> <option value="https://www.keslyns.com/pillowpurse-designs">Pillow/Purses</option> <option value="https://www.keslyns.com/specialty-items-designs">Specialties</option> <option value="https://www.keslyns.com/symstitch-designs">Sym-Stitch</option> </select>

For starters I have barely worked with javascripting with HTML.对于初学者,我几乎没有使用 HTML 进行 javascripting。 I am trying to create a search function that filters a list of items on a webpage.我正在尝试创建一个搜索功能来过滤网页上的项目列表。 Context: The site was originally created on squarespace so some options are limited, this is why I'm using their built in code blocks to accomplish this.上下文:该站点最初是在 squarespace 上创建的,因此某些选项是有限的,这就是我使用其内置代码块来完成此操作的原因。 The downside of this is that class/ IDs are hard to find.这样做的缺点是很难找到类/ID。 The page in question is https://www.keslyns.com/all-designs-details .有问题的页面是https://www.keslyns.com/all-designs-details

You can remove padding style of an element with a code similar to this您可以使用类似于此的代码删除元素的填充样式

document.getElementById("myDiv").style.padding = "0";

If there is no class or id associated with an element, then you can try to use document.querySelector() to target a specific element.如果没有与元素关联的类或 id,那么您可以尝试使用document.querySelector()来定位特定元素。

document.querySelectorAll() is used to match more than one element. document.querySelectorAll()用于匹配多个元素。

You can read more about it here你可以在这里阅读更多关于它的信息

Please also note that, this code must run after the elements are loaded onto the DOM.另请注意,此代码必须在元素加载到 DOM 后运行。 So, it is better to add your javascript to footer section of your page.因此,最好将您的 javascript 添加到页面的页脚部分。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM