我有一个 HTML table
元素,其中的行不仅包含纯文本字符,还包含单选按钮
、列表
等。
我想要的是,当用户将表格内容复制粘贴到 MS Excel 中(作为纯文本)时,单选按钮
应替换为它们的值(例如: “选中”和“未选中”),列表元素应替换为其选定的值等。
有没有办法在浏览器中实现这个功能? (最好不使用flash或java applet组件)
谢谢, 克里西
最佳答案
您可以使用 javascript(使用 jquery 这样的框架会更容易)来修改已复制的代码。一般过程是将单选按钮设置为不可见,然后根据其当前值在其位置添加文本。
有很多问题可以帮助了解实现细节:https://stackoverflow.com/search?q=%5Bjavascript%5D+copied+text&submit=search
更新:事实证明,您甚至不需要删除单选按钮,至少对于我的 Excel 版本来说是这样。这是一个工作示例(过度注释以解释发生的情况):
<!DOCTYPE html>
<html>
<head>
<style type='text/css'>
.radioCopyVal{
font-size: 0;
}
</style>
</head>
<body>
<form>
<table>
<tr>
<td><input type='radio' name='woots' id='oneWoot' checked/><span class='radioCopyVal'>Checked.</span></td>
<td><label for='oneWoot'>Woot.</label></td>
</tr>
<tr>
<td><input type='radio' name='woots' id='twoWoot' /><span class='radioCopyVal'>Not checked.</span></td>
<td><label for='twoWoot'>Woot. Woot.</label></td>
</tr>
</table>
</form>
<script src="jquery-1.7.2.min.js"></script>
<script type='text/javascript'>
$("#oneWoot").attr("checked", true); //on page load, make sure oneWoot is the default selection
$("input[name=woots]:radio").change(function(){ //anytime the radio buttons of name woots are changed...
$(".radioCopyVal").remove(); //remove the old radioCopyVal spans (to be replaced with new ones)
$("input[name=woots]:radio").each(function() { //for each radio button in name woots
if ($(this).attr("checked")) { //if the button is checked, add a span saying 'Checked.' after it. css is used to hide the span
$(this).after('<span class="radioCopyVal">Checked.</span>');
}else {
$(this).after('<span class="radioCopyVal">Not checked.</span>');
}
})
});
</script>
</body>
</html>
关于javascript - 带有单选按钮的 html 表复制粘贴到 xls 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12258520/