简体   繁体   English

使用Jquery来回切换内容

[英]Switch Content back and forth using Jquery

I have an app where when the user clicks on a table, assigned to btnAlternativeService, the content from this table swaps with another one. 我有一个应用程序,当用户点击一个表,分配给btnAlternativeService时,该表中的内容与另一个表交换。

The jQuery I have used involves creating two variables for each piece of content in the table, one to act as a JS selector and another to retain the original content. 我使用的jQuery涉及为表中的每个内容创建两个变量,一个用作JS选择器,另一个用于保留原始内容。 It looks like this: 它看起来像这样:

// Switch between the two services
  btnAlternativeService.on('click', function(){

    // Set original recommended text variables
    var serviceSubTextOriginal = $('.js-second-step .subtext-top').text();
    var serviceProductTitleOriginal = $('.js-second-step .product-title').text();
    var serviceMileageOriginal = $('.js-miles').text();
    var serviceRecommendationOriginal = $('.js-second-step .full-rec').text();

    // Set recommended text selector variables
    var serviceSubText = $('.js-second-step .subtext-top');
    var serviceProductTitle = $('.js-second-step .product-title');
    var serviceMileage = $('.js-miles');
    var serviceRecommendation = $('.js-second-step .full-rec');

    // Set original alternative variables
    var alternativeProductTitleOriginal = $('.js-alternative h3').text();
    var alterativeMilageOriginal = $('.js-miles-alternative').text();
    var alternativeSubTextOriginal = $('.js-alternative .alternative-subtext').text();

    // Set alternative selector variables
    var alternativeProductTitle = $('.js-alternative h3');
    var alterativeMilage = $('.js-miles-alternative');
    var alternativeSubText = $('.js-alternative .alternative-subtext');

    // Swap everything around
    serviceProductTitle.text(alternativeProductTitleOriginal);
    serviceMileage.text(alterativeMilageOriginal);
    serviceRecommendation.text(alternativeSubTextOriginal);
    alternativeSubText.text(serviceRecommendationOriginal);
    alternativeProductTitle.text(serviceProductTitleOriginal);
    alterativeMilage.text(serviceMileageOriginal);
  }); 

This seems very long winded - is there a better way for me to swap the content around? 这似乎很长时间 - 我有更好的方式来交换内容吗?

You can select the elements by order and create 2 collections and use the indices for setting the text contents: 您可以按顺序选择元素并创建2个集合,并使用索引设置文本内容:

var $first = $('.js-second-step .subtext-top, ...');
var $second = $('.js-alternative h3, ...');

$first.text(function(index, thisText) {
    // select the corresponding element from the second set
    var $that = $second.eq( index ), thatText = $that.text();

    $that.text( thisText );
    return thatText;
});

Use a function: 使用功能:

function switchContent(selector_1, selector_2){
   data_1 = $(selector_1).text()
   data_2 = $(selector_1).text()
   $(selector_1).text(data_2)
   $(selector_2).text(data_1)
 }

btnAlternativeService.on('click', function(){
  switchContent('.js-alternative h3', '.js-second-step .product-title') 
  switchContent('.js-miles-alternative', '.js-miles')    
  switchContent('.js-alternative .alternative-subtext', '.js-second-step .full-rec')
}); 

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

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