1

嗨,我需要能够滚动/单击一些项目符号并定位页面上其他位置的 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>
4

1 回答 1

1

您需要一个地方来存储文本,所以使用 data 属性将文本添加到元素中

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

然后,只需将'$('.description')'文本设置为'data-description'您单击的项目中的 aka'$(this)'

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

<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/

于 2012-07-05T14:09:32.023 回答