javascript - 如何更改菜单选项卡 html 的事件颜色?

标签 javascript html css

我想将导航栏中的事件菜单选项卡更改为紫色,将非事件选项卡更改为绿色。链接到 fiddle 上的代码 https://jsfiddle.net/jtsrwcu9/1/


        <div class="search-tab-content">
            <div class="search-bar">
                <input type="text" id="search-text" placeholder="Search Catalog" name="qu">
                <input type="image" alt="go" id="search-text-icon" title="Search" src="img.png">
            </div>
        </div>

我还希望搜索栏中的占位符根据选项卡更改

最佳答案

我这样做的方式是,

我将从一个没有 .selected 类的绿色按钮开始,然后在单击它时添加它。

这可以在 JS 中非常容易地完成...

如果你想使用jQuery,看看这个reference page ... 否则看看使用 onclick="function()" 调用函数事件。

然后 add the class当它被点击时。

编辑:我创建了一个 JSFiddle soloution根据OP的要求。 (请原谅 HTML 中的 JavaScript,我是 JSfiddle XD 的菜鸟)

<!-- Here is the buttons, I've gave the button I want to be selected on page first, here. That is done by giving it the selected class.-->

<button id="btn1" class="selected" onclick="click1()">First</button>
<button id="btn2" onclick="click2()">Second</button>

<!-- I've put the script in the HTML because I'm a JS Fiddle Noob -->
<script>
  // The buttons are saved into a Var so it can be referenced later.
  var btn1 = document.getElementById("btn1");
  var btn2 = document.getElementById("btn2");

  // This what happens when clicked on the FIRST button
  function click1() {
    btn1.classList.add("selected"); // give the selected class
    btn2.classList.remove("selected"); // take away the selected class
  }

  // This what happens when clicked on the SECOND button
  function click2() {
    btn2.classList.add("selected"); // give the selected class
    btn1.classList.remove("selected"); // take away the selected class
  }
</script>

关于javascript - 如何更改菜单选项卡 html 的事件颜色?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55787392/

相关文章:

javascript - 选择标签默认选择空白项

html - 发送 html/图像电子邮件的最佳方式是什么?

html - 可以将元素强制到隐式网格的最后一行吗?

javascript - 使网页上的磁贴按顺序点亮不同的颜色

html - 过渡效果不起作用

javascript - 切换最近的 div 位置

javascript - 如何将粘性 div 保持在固定标题下方

javascript - jQuery 画廊翻转与下一个和上一个按钮

javascript - d3 : confusion about selectAll() when creating new elements

php - 根据用户输入更新mysql数据库