javascript - onmouseover 并单击目标 div 并替换文本

标签 javascript html css

您好,我需要能够滚动/单击某些元素符号并定位页面(描述)上其他位置的 div 以替换其中的文本。有没有人有任何想法?,我真的很感激任何帮助 - 谢谢!

JS

$(document).ready(function(){
    $('.bullet').click(function(){
        $('.bullet').removeClass("active");
        $(this).addClass("active");
    });   
});

CSS

.bullet {
    display:block;
    margin: 0 0 0 3px;
    float: right;
    width:10px;
    height:10px;
    background:url(../images/bullets.jpg) 0 0 no-repeat;
}

.bullet:active {
    display:block;
    margin: 0 0 0 3px;
    float: right;
    width:10px;
    height:12px;
    background:url(../images/bullets.jpg) 0 -14px no-repeat;
}

.numbers a {
    display: block;
}

.numbers a div {
    font-family: HelveticaNeueLTPro-Roman, Arial, Helvetica, sans-serif;
    font-size: 9px;
    color: #6e6965;
    margin: -10px 0 0 0px;
    position: absolute;
    display:none;
}

.numbers a:hover div, .numbers a:focus div {
    display: block;
}

HTML

<div id="bullet-container"><div class="numbers">
    <span onmouseover="document.rollimg.src=image1.src;">
        <a class="bullet" tabindex="-1">
            <div>1</div>
        </a>
    </span>
    <span onmouseover="document.rollimg.src=image2.src;">
        <a class="bullet" tabindex="-1">
            <div>2</div></a>
    </span>
    <span onmouseover="document.rollimg.src=image3.src;">
        <a class="bullet" tabindex="-1">
            <div>3</div>
        </a>
    </span>
    <span onmouseover="document.rollimg.src=image4.src;">
        <a class="bullet" tabindex="-1">
            <div>4</div>
        </a>
    </span>
</div>

<div class="description">hello</div>

最佳答案

您需要一个地方来存储文本,因此使用数据属性将文本添加到元素

<a data-description="hello, I am the first bullet" class="bullet" tabindex="-1">1</a>

然后,只需将 '$('.description')' 文本设置为您正在单击的元素中的 'data-description' 也就是 ' $(这个)'

要自动选择第一个,您可以在文档准备好时在第一个 '.bullet' 上触发点击事件

<script type="text/javascript">
$(document).ready(function(){

  // Set up click event
  $('.bullet').click(function(){
    $('.bullet').removeClass("active");
    $(this).addClass("active");
    $('.description').text($(this).data('description'))
  });  

  // Click the first item
  $('.bullet:first').click();

});
</script>

工作示例

http://jsfiddle.net/blowsie/jtnsP/

关于javascript - onmouseover 并单击目标 div 并替换文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11345983/

相关文章:

Javascript 模块模式内存占用和性能

javascript - 使用 HOC 时在子函数中获取 props

javascript - 响应式导航栏下拉菜单(非 Bootstrap )

html - 单击菜单后如何添加CSS类并突出显示当前菜单

html - 我应该在哪里报告错误 - Firefox 或 Internet Explorer?

javascript - jQuery - 如何组合多个点击操作

javascript - JS odoo 10 中的函数返回 `undefined`

javascript - facebook 连接第三方小部件?

javascript - 如何将变量从 html 表单传递到 .js 文件?

css - Canvas 底部有空白并且滚动得太远