简体   繁体   English

将jQuery转换为Prototype JS

[英]Convert jQuery to Prototype JS

I need to convert the following jQuery into Prototype JS. 我需要将以下jQuery转换为Prototype JS。

jQuery("button.btn-transcript").click(function() {
  tsTarget = jQuery(this).attr("data-target");
  if (jQuery(this).hasClass("collapsed")) {
    jQuery(tsTarget).show(200);
    jQuery(this).removeClass("collapsed");
    jQuery(this).attr("area-expanded","true");
  } else {
    jQuery(tsTarget).hide(200);
    jQuery(this).addClass("collapsed");
    jQuery(this).attr("area-expanded","false");
  }
});

I gave it a try but I'm not too good with JS prototype. 我试了一下,但我对JS原型不太好。 Am I heading in the right direction? 我正朝着正确的方向前进吗?

$("button.btn-transcript").on('click', 'button.btn-transcript', function(event, el)) {
  transTarget = $(this).readAttribute("data-target");
  function(event,el) {
    if($(this).hasClassName("collapsed")) {
      $("transTarget").show();
      $(this).removeClassName("collapsed");
      $(this).writeAttribute("area-expanded", "true");
    } else {
      $("transTarget").hide();
      $(this).addClassName("collapsed");
      $(this).writeAttribute("area-expanded", "false");
    }
  }

Try this: 尝试这个:

$(document).on('click', 'button.btn-transcript', function(evt, elm) {
  var tsTarget = $$(elm.readAttribute('data-target')).first();
  elm.toggleClassName('collapsed');
  tsTarget.toggle();
  elm.writeAttribute('aria-expanded', 
    (elm.readAttribute('aria-expanded') == 'true' ? 'false' : 'true'));
});

It's not going to work 100% the same, because hide and show in Prototype (which are collapsed here into the one-liner toggle ) are instantaneous. 它不会100%相同,因为Prototype中的隐藏和显示(这里折叠成单行toggle )是瞬间完成的。 If you want the item to transition over 200ms the way you had written yours, you will need to use CSS transition effects. 如果您希望项目按照您编写的方式转换超过200毫秒,则需要使用CSS过渡效果。

If your button controls more than one item (if more than one element in the DOM matches what you have entered in the data-target attribute), then you would change this very slightly: 如果您的按钮控制多个项目(如果DOM中的多个元素与您在data-target属性中输入的元素匹配),那么您将稍微更改一下:

$(document).on('click', 'button.btn-transcript', function(evt, elm) {
  var tsTargets = $$(elm.readAttribute('data-target'));
  elm.toggleClassName('collapsed');
  tsTargets.invoke('toggle');
  elm.writeAttribute(
    'aria-expanded', 
    (elm.readAttribute('aria-expanded') == 'true' ? 'false' : 'true')
  );
});

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

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