javascript - 如何将jquery省略号应用于div

标签 javascript jquery html css accordion

$('.more2').each(function() {       
    var showChar = 150;
    var content = $(this).html();
        if(content.length > showChar) {
            var c = content.substr(0, showChar);
            var h = content.substr(showChar, content.length - showChar);
            var html = c + '<span class="moreellipses">' + ellipsestext+ ' </span><span class="morecontent"><span>' + h + '</span>  <a href="" class="morelink">' + moretext + '</a></span>';
            $(this).html(html);
    }
});

我正在尝试为我的 div 下拉菜单构建一些 jquery Accordion 效果;请看例子JSFIDDLE .这里我想要的实际上是点击('+')按钮我想显示内容的扩孔部分。那时同一行中的其他 div 应该采用与事件 div 相同的高度,但我们不想显示非事件 div 中的内容。

最佳答案

准备好尝试添加到您的文档中:

$('a.morelink').click(function(){
    $(this).parent().find('span').toggle();
    $(this).parent().parent().find('span.moreellipses').toggle();
    $(this).parent().find('a').text($(this).text() == '[+]' ? "[-]" : "[+]");
    return false;
});

并替换:

$(".row1").height(maxHeight);

$(".row1").css({'min-height':maxHeight,'height':'auto'});

所以你的代码可以是:

	$(document).ready(function() {
	  var maxHeight = 0;
	  $(".row1").each(function() {
	    if ($(this).height() > maxHeight) {
	      maxHeight = $(this).height();
	    }
	  });
	  $(".row1").css({
	    'min-height': maxHeight,
	    'height': 'auto'
	  });
	});
	$(document).ready(function() {
	  var ellipsestext = "...";
	  var moretext = "[+]";
	  var lesstext = "[-]";
	  $('.more1').each(function() {
	    var showChar = 172;
	    var content = $(this).html();

	    if (content.length > showChar) {

	      var c = content.substr(0, showChar);
	      var h = content.substr(showChar, content.length - showChar);

	      var html = c + '<span class="moreellipses">' + ellipsestext + ' </span><span class="morecontent"><span>' + h + '</span>  <a href="" class="morelink">' + moretext + '</a></span>';

	      $(this).html(html);
	    }

	  });
	  $('.more2').each(function() {
	    var showChar = 150;
	    var content = $(this).html();

	    if (content.length > showChar) {

	      var c = content.substr(0, showChar);
	      var h = content.substr(showChar, content.length - showChar);

	      var html = c + '<span class="moreellipses">' + ellipsestext + ' </span><span class="morecontent"><span>' + h + '</span>  <a href="" class="morelink">' + moretext + '</a></span>';

	      $(this).html(html);
	    }

	  });

	  $('a.morelink').click(function() {
	    $(this).parent().find('span').toggle();
	    $(this).parent().parent().find('span.moreellipses').toggle();
	    $(this).parent().find('a').text($(this).text() == '[+]' ? "[-]" : "[+]");
	    return false;
	  });
	});
spk_desc {
  font-size: 11px;
  line-height: 15px;
  padding: 15px;
  position: relative;
  top: 0;
}
.speaker_content_text_alt {
  background: #eeeeee none repeat scroll 0 0;
  padding: 5px;
  position: relative;
  width: 200px;
}
.speaker_content_text {
  background: #dddddd none repeat scroll 0 0;
  padding: 5px;
  position: relative;
  width: 200px;
}
.fl {
  float: left;
}
a {
  color: #0254EB
}
a:visited {
  color: #0254EB
}
a.morelink {
  text-decoration: none;
  outline: none;
}
.morecontent span {
  display: none;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="speaker_content" class="visible-desktop">
  <div id="row1">
    <div class="speaker_content_text_alt   fl">
      <a name=""></a>
      <div class="speaker_name">
        <p style=" font-size:25px; padding top: 10px; ">ironMAn
          <br/>
        </p>
      </div>
      <div class="spk_desc row1">
        <p style="line-height:19px;" class="comment more1">I´ve create a template for meteor.js, what I want is to apply a jQuery effect for an onclick event on tran_button, the idea with the effect is that div associated to button disappear. I don´t get any error through meteor console, or firebug console.
          He has written apps for the iPhone and the iPad since the SDKs first appeared and has written programs for the Mac all the way back to System 7.
          <br>Daniel presents iPhone, Cocoa, and Swift training and consults through his company He is the host of the and are available on the Editors Cut website.</p>
      </div>
    </div>
    <div class="speaker_content_text   fl">
      <a name=""></a>
      <div class="speaker_name">
        <p style="font-size:25px;padding-top:10px; ">
          Supeeer man
          <br/>
        </p>
      </div>
      <div class="spk_desc row1">
        <p style="line-height:19px;" class="comment more2">
          I understand this is a privilege that is earned by gaining 2,000 reputation on the site. As far as I can see, that is the only requirement - there doesn't seem to be any restrictions on where you get that rep from. For instance, I got mine mostly through
          answers and a few questions - I think I may have made one suggested edit before gaining this privilege which allows me to edit anything, without peer review..
          <br>Please note - I'm not trying to suggest that there are roving bands of 2K users rampantly defiling posts left and right; rather this is more focussed on introducing a learning curve for edits, rather than allowing users who may have no idea
          how to edit a post edit anything.
        </p>
      </div>
    </div>

  </div>
</div>

关于javascript - 如何将jquery省略号应用于div,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31293884/

相关文章:

javascript - 动态添加的对象属性未在 Javascript 中添加

javascript - 使用 Raw javascript 实现 fadeIn fadeOut 效果

jquery - JSTREE - 刷新 jstree,以便重新运行 'bind("returned.jstree")' 内的所有代码

javascript - 如何阻止函数执行直到 json 返回结果?

jquery - $(document).ready 在外部函数中调用?

html - 大型 Bootstrap 堆叠导航丸?

html - 如何防止两个元素换行?

javascript - 将回调返回值分配给变量

javascript - 如何将字典数组整理成数组字典?

javascript - 具有相同类可拖动的 Div