怎么实现豆瓣那样复制文章时会出现版权信息
直接看豆瓣代码啦
参数 .article-content
是指定复制那个元素内的内容,会自带版权信息
元素 article-title
是文章标题的标签
$(function(){
_REVIEW_COPY_OPTIONS = {
targetNode: ".article-content",
handleCopy: function(e, t) {
var n = ["内容来源-三三淘资源", "名称:" + $('.article-title').text(), "地址:" + $('.article-title > a').attr('href'), "\n\n"].join("\r\n");
return n + e
}
},
function (e) {
if ("undefined" != typeof e) {
var t = function() {
return window.getSelection ? window.getSelection().toString() : document.selection && "Control" != document.selection.type ? document.selection.createRange().htmlText.replace(/<br>/gi, "\r\n") : ""
},
i = function(e, t) {
if (e) try {
if (e.setData("Text", t), e.getData("Text").length > 0) return
} catch(i) {
if (e.setData("text/plain", t), e.getData("text/plain").length > 0) return
}
var i = $("<div>").css({
position: "absolute",
left: "-99999px"
}).appendTo("body");
i.html(t),
window.getSelection().selectAllChildren(i[0]),
window.setTimeout(function() {
i.remove()
},
200)
};
$("body").delegate(e.targetNode, "copy cut",
function(n) {
var a = t();
if (! (a.length <= 42)) {
var o = e.handleCopy && e.handleCopy(a, $(this)) || null;
if (o) {
n.preventDefault();
var r = n.originalEvent.clipboardData || window.clipboardData;
i(r, o)
}
}
})
}
} (_REVIEW_COPY_OPTIONS)
})
评论