我看过几个网站,当你从他们的网站上复制和粘贴文字时,结果是这样的:
<text here> - From <website name here>
我想知道该怎么做。我尝试过使用alt参数和其他参数。提前谢谢。
答案 0 :(得分:0)
这并不像看起来那么简单。我之前已经这样做了所以我将告诉你如何使用jquery和rangy选择库(https://code.google.com/p/rangy/)
基本上你这样做:
这是一个小提琴和代码:
$("body").on("copy", function() {
var selection = rangy.getSelection();
var ranges = selection.getAllRanges();
var container = $("<div>");
for (var i = 0; i < ranges.length; i++)
container.append(ranges[i].cloneContents());
container.append(" - whatever text you want to append");
var wnd = $(window);
var scrollTop = wnd.scrollTop();
var scrollLeft = wnd.scrollLeft();
var offscreen = $("<div>").css({
position: "absolute",
left: "-1000px",
width: "1px",
height: "1px",
overflow: "hidden"
}).appendTo("body");
offscreen.css({ top: scrollTop });
offscreen.append(container);
selection.selectAllChildren(container[0]);
event.stopPropagation();
setTimeout(function() {
selection.setRanges(ranges);
offscreen.detach(); // offscreen.remove() will remove events attached to the original elements (not cloned elements) on IE8
wnd.scrollTop(scrollTop);
wnd.scrollLeft(scrollLeft);
}, 0);
});