我想复制任何span部分中的文本,并在CTRL + V上的任何地方使用它。但是下面的代码只适用于输入,而不适用于span。
代码如下:
$scope.copyCode = function(name, code, index) {
$("#buttonCopyClass" + index).css("display", "none");
$("#buttonCopiedClass" + index).css("display", "block");
ga('send', 'event', 'userCopiedCoupen', name, code);
copyToClipboard(document.getElementById("copyTarget"));
return event.stopPropagation();
};
copyToClipboard = function(elem) {
var currentFocus, e, isInput, origSelectionEnd, origSelectionStart, succeed, target, targetId;
targetId = '_hiddenCopyText_';
isInput = elem.tagName === 'INPUT' || elem.tagName === 'TEXTAREA';
origSelectionStart = void 0;
origSelectionEnd = void 0;
if (isInput) {
console.log(78);
target = elem;
origSelectionStart = elem.selectionStart;
origSelectionEnd = elem.selectionEnd;
} else {
console.log(7889);
target = document.getElementById(targetId);
if (!target) {
target = document.createElement('textarea');
target.style.position = 'absolute';
target.style.left = '-9999px';
target.style.top = '0';
target.id = targetId;
document.body.appendChild(target);
}
target.textContent = elem.textContent;
}
currentFocus = document.activeElement;
target.focus();
target.setSelectionRange(0, target.value.length);
succeed = void 0;
try {
succeed = document.execCommand('copy');
} catch (error1) {
e = error1;
succeed = false;
}
if (currentFocus && typeof currentFocus.focus === 'function') {
currentFocus.focus();
}
if (isInput) {
elem.setSelectionRange(origSelectionStart, origSelectionEnd);
} else {
target.textContent = '';
}
return succeed;
};
以上代码我习惯于获取输入场的文本,但我需要这个与sapn feild合作。