【发布时间】:2015-02-22 01:39:30
【问题描述】:
我有一个包含两列的表,第二列有时包含大文本,因此我只想显示前 100 个字符并放置一个显示更多链接以显示剩余文本。你可以在这里看到我正在处理的表http://jsfiddle.net/j11mj21x/。
为此,我正在使用此链接 (http://viralpatel.net/blogs/dynamically-shortened-text-show-more-link-jquery/) 中提供的代码,我将其放入文件 show_less_more.js 中:
(function($) {
$.fn.shorten = function (settings) {
var config = {
showChars: 100,
ellipsesText: "...",
moreText: "more",
lessText: "less"
};
if (settings) {
$.extend(config, settings);
}
$(document).off("click", '.morelink');
$(document).on({click: function () {
var $this = $(this);
if ($this.hasClass('less')) {
$this.removeClass('less');
$this.html(config.moreText);
} else {
$this.addClass('less');
$this.html(config.lessText);
}
$this.parent().prev().toggle();
$this.prev().toggle();
return false;
}
}, '.morelink');
return this.each(function () {
var $this = $(this);
if($this.hasClass("shortened")) return;
$this.addClass("shortened");
var content = $this.html();
if (content.length > config.showChars) {
var c = content.substr(0, config.showChars);
var h = content.substr(config.showChars, content.length - config.showChars);
var html = c + '<span class="moreellipses">' + config.ellipsesText + ' </span><span class="morecontent"><span>' + h + '</span> <a href="#" class="morelink">' + config.moreText + '</a></span>';
$this.html(html);
$(".morecontent span").hide();
}
});
};
})(jQuery);
$(document).ready(function(){$(".descriptionText").shorten();});
我是这样使用它的:
<script src="show_less_more.js"></script>"
每一行的 HTML 都是这样的:
<tr>
<th>
course1
</th>
<td> <div class="descriptionText">Description of the course</div></td>
</tr>
我还为越来越少的链接添加了 CSS:
a {
color: #0254EB
}
a:visited {
color: #0254EB
}
a.morelink {
text-decoration:none;
outline: none;
}
.morecontent span {
display: none;
}
当我在 sfiddle 中执行此操作时,效果非常好,您可以在此处看到 http://jsfiddle.net/j11mj21x/2/ 但是,当我用 python 渲染我的表时,我什么也没得到。 我认为问题在于我没有成功将我的 JS 页面加载到 html 页面中,因为当我点击它时它什么也没有。 这是在我的 html 页面中呈现的内容:
....
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script><!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/css/bootstrap.min.css"><!-- Optional theme -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/css/bootstrap-theme.min.css"><!-- Latest compiled and minified JavaScript --><script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/js/bootstrap.min.js">
</script><script src="showLessMore.js"></script>
....
谁能告诉我这背后的问题是什么,因为我在与 python 中的文件生成器相同的文件夹中有“show_less_more.js”?
提前谢谢你!
【问题讨论】:
-
您可以通过在 jsfiddle 中设置此功能来更轻松地提供帮助。我看到您在要应用此功能的表上执行了此操作,但它不包括您寻求帮助的任何功能。谢谢。
-
谢谢...这很奇怪。它仅适用于 HTML,我看到您如何在 document.click 上设置事件以及子目标以支持动态添加的元素。
-
如果您只尝试 document.click 会怎样?传入事件,并检查 event.currentTarget.hasClass('more') ..... 是否解决?
-
我认为我使用包含我的 JS 函数的 javascript 文件的方式存在问题。因为当我单击它查看里面的内容时,即使该文件与网页位于同一文件夹中,我也没有得到该文件
-
只需将一些控制台日志放入该文件中,然后查看您是否在网络检查器中看到它们。
标签: javascript jquery html css show-hide