-1

我有一个画廊,标题应该是我的标题。现在我想要画廊外的标题在一个单独的 div 中。这就是我通过图像获取标题的方式。但是如何将显示图像的标题附加到单独的 div 中?

var appendCaptions = function(){
        // cycle through each child
        slider.children.each(function(index){
            // get the image title attribute
            var title = $(this).find('img:first').attr('title');
            // append the caption
            if (title != undefined && ('' + title).length) {
                $(this).append(title);
            }
        });
    }


<ul class="bxslider">
              <li><img src="images/1.jpg" title="Happy trees" /></li>
              <li><img src="images/1.jpg" /></li>
              <li><img src="images/1.jpg" title="Happy tre333es" /></li>
              <li><img src="images/1.jpg" title="Happy treeffees" /></li>
            </ul>
<div class='title'></div>

我希望标题显示在 div 标题内;我使用的滑块是 bxslider

4

3 回答 3

1

您需要像在每个迭代器中一样中断循环,因此请尝试:

var appendCaptions = function(){
        // cycle through each child
        slider.children.each(function(index){
            // get the image title attribute
            if( $(this).is(':visible')){
            var title = $(this).find('img:first').attr('title');
            // append the caption
            if (title != undefined && ('' + title).length) {
                $('#myDiv').append(title);
                return false;
            }
          }
       });
    }
于 2013-07-05T10:02:35.777 回答
0

放置一些 HTML。

我假设您正在尝试:

  • 获取每个滑块图像的标题,
  • 将标题附加到作为此图像容器的每个 div。

也许尝试这样的事情:

var appendCaptions = function(){
        // cycle through each child
        slider.children.each(function(index){
        // get the image title attribute
        var title = $(this).find('img:first').attr('title');
        // append the caption
        if (title != undefined && ('' + title).length) {
            $(this).parents("div.singleImgContainer").append(title);
        }
    });
}

这将找到类 singleImgContainer 的 div 并将标题放入其中。

于 2013-07-05T10:12:25.130 回答
0

这是你要找的吗?

$("#myTitleDiv").html(title);

jQuery 文档非常棒。

于 2013-07-05T10:03:21.143 回答