javascript - 更改弹出窗口中所选部分的背景颜色

标签 javascript html css

我创建了一个弹出窗口,如果我在其中单击问号图像,它能够创建一个包含文本列表的框。它的 fiddle 是 here

我希望文本看起来像下面这样:

enter image description here

I am able to create a box containing list of text as shown in the fiddle but I am not sure how to change the background color of the selected portion text (eg: Support, Contact, Demo) .

例如,如果我选择或单击或将鼠标指向Support,则Support 文本部分的背景颜色应为橙色,其余部分应为橙色为黑色或如果我选择或单击或将鼠标指向 Contact,则其余文本的背景颜色应为黑色,Contact 文本具有橙色背景。

以下是来自 fiddle 的 HTML 和 CSS 片段:

HTML 片段:

<!DOCTYPE html>
<html>
<body style="text-align:center">

<br>
<br>
<br>
<br>
<br>
<br>
<div class="popup" onclick="myFunction()"><img src="https://s27.postimg.org/40rb1a2ir/questionmark2.jpg" alt="homeicon">
<div class="popuptext" id="div">
     <div><span id="myPopup">Support</span><br/></div>
     <div><span id="myPopup1">Contact</span><br/></div>
     <div><span id="myPopup2">Demo</span><br/></div>
</div>
</div>


<script>
// When the user clicks on div, open the popup
function myFunction() {
   var popup = document.getElementById('div');
popup.classList.toggle('show');
}
</script>

</body>
</html>

CSS 片段:

/* Popup container - can be anything you want */
.popup {
    position: relative;
    display: inline-block;
    cursor: pointer;
    -webkit-user-select: none;
    -moz-user-select: none;
    -ms-user-select: none;
    user-select: none;
}

/* The actual popup */
.popup .popuptext {
    visibility: hidden;
    width: 160px;
    background-color: #FD8B00;
    color: #fff;
    text-align: left;

    padding:8px 10px 10px 120px;
    position: absolute;
    z-index: 1;
    bottom: -225%;
    left: -52px;
    margin-left: -80px;
}

/* Popup arrow */
.popup .popuptext::after {
    content: "";
    position: absolute;
    top: -10%;
    left: 50%;
    margin-left: -5px;
    border-width: 6px;
    border-style: solid;
    border-color:#FD8B00 transparent transparent transparent;
}

/* Toggle this class - hide and show the popup */
.popup .show {
    visibility: visible;
    -webkit-animation: fadeIn 1s;
    animation: fadeIn 1s;
}

/* Add animation (fade in the popup) */
@-webkit-keyframes fadeIn {
    from {opacity: 0;} 
    to {opacity: 1;}
}

@keyframes fadeIn {
    from {opacity: 0;}
    to {opacity:1 ;}
}

最佳答案

所以我在下面的回答将告诉您如何在单击按钮时更改背景颜色。假设您有以下 CSS 代码。

#div div.selected {
  background-color: black;
}

然后,当用户点击一个按钮时,您需要删除之前选择的按钮并更改当前选择按钮的背景颜色。您可以使用下面的 jQuery 代码实现这一点:

$(document).ready(function(){
    $('#div div').click(function(){
    $('#div div.selected').removeClass('selected');
    $(this).addClass("selected");
  });
});

您还可以查看 Fiddle ..希望对您有所帮助..

关于javascript - 更改弹出窗口中所选部分的背景颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41176236/

相关文章:

javascript - 根据里面的信息调整div的大小

javascript - HTML/JavaScript 代码错误

javascript - 如何添加显示:inline-block to jQuery fadeIn

css - 使用@import 语法的自定义网络字体

javascript - TinyMCE-本 map 片上传

javascript - 使第一个子项和最后一个子项在 IE8 中工作

javascript - 页面加载时函数返回 NaN,但更改/更新时可以正常工作,如何修复它?

HTML5 元标记 : if you remove a security meta tag, 浏览器是否相应更新?

css - 边框CSS,颜色一定百分比

javascript - 如何强制 HTML 页面具有水平滚动条