简体   繁体   English

如何更改div的html并向其中添加动态内容

[英]How to change the html of a div and add dynamic content to it

I have a slider working with images and variables set that grab data from that slide. 我有一个使用图像和变量集的滑块,可以从该幻灯片中获取数据。 That data is manipulated by me to create certain functionality. 我操纵这些数据来创建某些功能。

I currently have a set of images that move across(slider) on my body of the page I have a social bar that is always foxed to the right of the page. 我目前在页面的主体上有一组在(滑块)上移动的图像,我的社交栏始终位于页面的右侧。

What I am trying to do is grab the data and change the urls, and text inside the social buttons each time the slide is changed using the callback function provided by the slider. 我想要做的是每次使用滑块提供的回调函数更改幻灯片时,都获取数据并更改url和社交按钮内的文本。 Here is my code: 这是我的代码:

var $mainContainer = $('div#container');
  var $tmbContainer = $('.rsThumbsContainer');
  var $tmbContainerT = $('.rsThumbsContainer').find('.rsThumb');
  var $slider = $('#gallery-t-group');
  var $body = $('body');
  var $close = $('<div id="close"><p class="icon-close"></p></div>');        

$tmbContainerT.each(function () {
      //console.log(slider.currSlide);
        $(this).on('click', function(event) {       
            $('body').addClass('rsSlider-active');
            sliderR();
            var $gallery = $('#gallery-t-group').find('.portfolio');

            $gallery.each(function () {
                var src = this.href;
                var content = $(this).find('.perma_link').val();
                var textContent = $(this).find('img').attr('alt'); // same as $(this.element).attr("title"); before you modify it using this.title +=
                var image = $(this).find('img').attr('src'); // this is the URL of the thumbnail
                var imageAlt = $(this).find('img').attr('alt');
                var $tumbl = $(this).find('a.tumbl-button');
                $tumbl.attr("href","http://www.tumblr.com/share/photo?source="+ encodeURIComponent(image) +"&caption="+ encodeURIComponent(textContent) +"&clickthru="+ encodeURIComponent(content) +"");

                slider.ev.on('rsAfterSlideChange', function(event) {
                $('#twit').html('<div id="twitter" data-url="'+content+'" data-text="'+textContent+'"></div>');
            $('#twitter').attr("data-url",content);
            $('#twitter').attr("data-text",textContent);
            $('#twitter').sharrre({
                    share: {
                        twitter: true
                    },
                    template: '<a class="box" href="#"><div class="count" href="#">{total}</div><div class="share"><span></span>Tweet</div></a>',
                    enableHover: false,
                    enableTracking: true,
                    buttons: { twitter: {via: '_JulienH'}},
                    click: function(api, options){
                        api.simulateClick();
                        api.openPopup('twitter');
                    }
                });
            });

            });
            slider.updateSliderSize(true);
        });

      });

This is where all my social buttons get added: 这是我所有社交按钮的添加位置:

<div class="socialbar-vertical">
<div id="socio-box">
<div id="twit"></div>
  <div id="facebook" data-url="http://sharrre.com/" data-text="Make your sharing widget with Sharrre (jQuery Plugin)"></div>
  <div id="googleplus" data-url="http://sharrre.com/" data-text="Make your sharing widget with Sharrre (jQuery Plugin)"></div>
</div>
</div>

Currently this is all I have that changes the content of a div but it always uses the first clicked div: 目前,这就是我更改div内容的全部方法,但它始终使用第一个单击的div:

$('#twit').html('<div id="twitter" data-url="'+content+'" data-text="'+textContent+'"></div>');

what I want to do is keep changing the attr's of that div everytime the slide changes. 我想做的就是每次幻灯片更改时都不断更改该div的attr。

Am I doing something completely wrong here or missing a trick? 我在这里做的事情完全错误还是缺少技巧?

a.) Why dont you just store the #twit.attr before you replace the div and then assign the value after replacement. a。)为什么在替换div之前不存储#twit.attr,然后在替换后分配值。

//Make a copy of all attributes of #twit
//TODO Replace #twit
//TODO set all attributes to the new #twit

b.) Another option is to stop replacing #twitter completely and instead re-using it (including its children). b。)另一个选择是停止完全替换#twitter,而重新使用它(包括其子代)。 Might be less resource consuming. 可能会减少资源消耗。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM