[英]Adding an event callback function to a button with a Font Awesome icon inside
I have a search bar on my header that I made hidden by default with a button next to it to show/hide it depending on certain conditions.我的标题上有一个搜索栏,默认情况下我将其隐藏,旁边有一个按钮,可根据特定条件显示/隐藏它。 I made the button content using Font Awesome where I put a search icon if the search bar is hidden to indicate "show search", and a right chevron icon if the search bar is shown to indicate "hide search."
我使用 Font Awesome 制作了按钮内容,如果搜索栏隐藏以指示“显示搜索”,我会放置一个搜索图标,如果显示搜索栏以指示“隐藏搜索”,则放置一个右 V 形图标。
The way I wrote my js is so that upon clicking the button, if the event target contains certain classes, the search bar will show/hide and the problem is that the event target can be either the i
element (Font Awesome) or the button
element.我编写我的 js 的方式是,在单击按钮时,如果事件目标包含某些类,搜索栏将显示/隐藏,问题是事件目标可以是
i
元素(Font Awesome)或button
元素。 This isn't a problem when clicking to show the search bar, but when clicking to hide it, I can't seem to get the callback function to trigger when the event target is the button
element.单击以显示搜索栏时这不是问题,但是当单击以隐藏它时,当事件目标是
button
元素时,我似乎无法触发回调函数。 I really hope I'm making sense.我真的希望我说得有道理。
Here's my JSFiddle -- https://jsfiddle.net/42uoLsrk/2/这是我的 JSFiddle -- https://jsfiddle.net/42uoLsrk/2/
function showHideSearch(e) {
e.preventDefault;
const searchBar = document.querySelector("#search");
const searchButton = document.querySelector("#search-button");
if (
e.target.classList.contains("show-btn") ||
e.target.classList.contains("fa-search")
) {
searchBar.classList.add("show-search");
searchButton.innerHTML = `<i class="fas fa-chevron-right"></i>`;
} else if (
e.target.classList.contains("fa-chevron-right") ||
e.target.innerHTML == `<i class="fas fa-chevron-right"></i>`
) {
searchBar.classList.remove("show-search");
searchButton.innerHTML = `<i class="fas fa-search"></i>`;
}
}
Line 24 in JS in particular is where I'm struggling:特别是 JS 中的第 24 行是我挣扎的地方:
e.target.innerHTML == `<i class="fas fa-chevron-right"></i>`
I feel like it really should work, since when the search bar is shown, the innerHTML
of the button
element is exactly that, but it's not working.我觉得它真的应该有效,因为当显示搜索栏时,
button
元素的innerHTML
正是如此,但它不起作用。
Thanks in advance.提前致谢。
I created an updated version of the jsfiddle here .我在这里创建了 jsfiddle 的更新版本。 I fixed a few things, first since you're only getting an element by I'd just use
getElementById
.我修复了一些问题,首先因为您只通过我使用
getElementById
获取元素。 Next the button will always have a fairly chance of being pressed regardless of whether you are closing or opening the input field, so you should create a variable that checks whether the input is open or closed.接下来,无论您是关闭还是打开输入字段,按钮总是有相当的机会被按下,因此您应该创建一个变量来检查输入是打开还是关闭。 If it's open and the button is clicked close it and vice versa.
如果它是打开的并且单击按钮关闭它,反之亦然。
const searchDiv = document.getElementById("search-div"); let open = false; // ADD EVENT LISTENERS searchDiv.addEventListener("click", showHideSearch); // FUNCTION: SHOW/HIDE SEARCH BAR ON BUTTON CLICK function showHideSearch(e) { e.preventDefault; const searchBar = document.querySelector("#search"); const searchButton = document.querySelector("#search-button"); if ( (e.target.classList.contains("show-btn") || e.target.classList.contains("fa-search")) && !open ) { searchBar.classList.add("show-search"); searchButton.innerHTML = `<i class="fas fa-chevron-right"></i>`; open = true; } else if ( ( e.target.classList.contains("fa-chevron-right") || e.target.id == "search-button") && open ) { searchBar.classList.remove("show-search"); searchButton.innerHTML = `<i class="fas fa-search"></i>`; open = false; } }
/* GENERAL */ :root { --light-color: #ccc; --lighter-color: #f4f4f4; --dark-color: #333; --darker-color: #222; --brand-color: #ff4; --danger: #f44; --danger-dark: #c00; } * { margin: 0; padding: 0; box-sizing: border-box; } body { background: var(--dark-color); color: var(--light-color); font-family: "Trebuchet MS"; } ul li { list-style: none; } button, input { outline: none; } /* UTILITY */ .highlight { color: var(--brand-color); } .show-search { width: 100% !important; border: black 2px solid; padding: 0.6rem 1rem; } /* HEADER */ header { background: var(--darker-color); display: flex; flex-direction: row; align-items: center; text-align: center; justify-content: space-between; padding: 1.8rem 6rem; width: 100%; } #logo { font-size: 2.4rem; font-weight: 200; } #search-div { width: auto; height: auto; display: flex; gap: 0.4rem; } .show-btn { padding: 0.6rem 0.7rem; background: var(--light-color); border-radius: 5px; border: none; transition: ease-in 300ms; font-size: 1.2rem; cursor: pointer; height: 100%; width: 3rem; } .show-btn:hover { background: var(--brand-color); transition: ease-in 300ms; } #search { width: 0; height: 100%; background: var(--lighter-color); color: var(--darker-color); font-size: 1.2rem; border-radius: 2px; transition: ease-in 300ms; border: none; }
<head> <script src="https://kit.fontawesome.com/3ad7573e76.js" crossorigin="anonymous"></script> </head> <body> <header> <div id="logo-div"> <h1 id="logo"> <span class="highlight"><i class="fas fa-user-friends"></i></span> My<span class="highlight">Contact</span>List </h1> </div> <div id="search-div"> <button id="search-button" class="show-btn"><i class="fas fa-search"></i></button> <input id="search" type="text" placeholder="Search contacts..."> </div> </header></body>
If you require any more clarification please don't hesitate to ask.如果您需要更多说明,请随时提出。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.