简体   繁体   English

jQuery Collapser在DIV中隐藏或显示内容

[英]jQuery Collapser hide or show content inside the DIV

My question is related with " jQuery – Collapser plugin " ( http://www.aakashweb.com/jquery-plugins/collapser/ ):- 我的问题与“ jQuery – Collapser插件 ”( http://www.aakashweb.com/jquery-plugins/collapser/ )有关:

I am using the following script to hide or show content inside the DIV with CLASS name 'shrink'. 我正在使用以下脚本在CLASS名称为“ shrink”的DIV中隐藏或显示内容。 It works fine. 工作正常。

<script type="text/javascript">
$(document).ready(function() {
    $('.shrink').collapser({
        mode: 'words',
        truncate: 80,
        ellipsis: ' ... '
    });
});
</script>

But, In the BODY I use a javascript to print some content inside DIV with a table to hide or show content inside <tr><td><div class="shrink"></div></td></tr> . 但是,在BODY中,我使用JavaScript在DIV中打印一些内容,并使用表格隐藏或显示<tr><td><div class="shrink"></div></td></tr> Now, hide or show content does not works. 现在,隐藏或显示内容无效。 ( sample script is given below, Note: There is no line break. It is just for explanation only. ) 下面给出了示例脚本,注意:没有换行符。仅用于说明。

<script type="text/javascript">
var dataTable = document.getElementById('Result');
var content = "<table class='tablesorter'>
<thead>
  <tr>
    <th>ID</th>
    <th>Title</th>
  </tr>
</thead>
<tbody>
  <tr>
    <td>123</td>
    <td><div class='shrink'>Some long content....</div></td>
  </tr>
</tbody>
</table>";
dataTable.innerHTML = content;
</script>

<div id="Result"></div>

Also, I tried with a simple statement as below. 此外,我尝试了一个简单的语句,如下所示。 It does not works. 它不起作用。

var content = "<div class='shrink'>Some long content....</div>";

you have to be careful with line breaks in javascript strings. 您必须注意javascript字符串中的换行符。 note how syntax highlighting stops on the second line. 请注意语法突出显示如何在第二行停止。 you have to either escape them or concat them. 您必须逃脱它们,或者迷住它们。

escaping.. 转义..

var content = "<table class='tablesorter'>\
<thead>\
  <tr>\
  ....
</table>";

Note that if you escape thelinebreaks, there can't be any trailing spaces after the \\ . 请注意,如果您逃脱了换行符,则\\之后不能有任何尾随空格。

concatting.. 凝结..

var content = "<table class='tablesorter'>"+
"<thead>"+
  "<tr>"+
  ....
"</table>";
<script type="text/javascript">
function shrink() {
  $('.shrink').collapser({
    mode: 'words',
    truncate: 80,
    ellipsis: ' ... '
  });
}
function display () {
  var dataTable = document.getElementById('Result');
  var content = "<table class='tablesorter'>
  <thead>
    <tr>
      <th>ID</th>
      <th>Title</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>123</td>
      <td><div class='shrink'>Some long content....</div></td>
    </tr>
  </tbody>
  </table>";
  dataTable.innerHTML = content;
}
setTimeout(function(){ display(); shrink(); }, 1000);
</script>

<div id="Result"></div>

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

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