javascript复制与粘贴字符串操作
//javascript 去空格
String.prototype.Trim = function()
{
return this.replace(/(^\s*)|(\s*$)/g, "");
}
//javascript 的复制也粘贴
<html>
<head>
</head>
<body>
<script>
function CopyCode(key)
{
var trElements = document.all.tags("tr");//获取tr元素
var i;
for(i = 0; i < trElements.length; ++i)
{
if(key.parentElement.parentElement.parentElement == trElements[i].parentElement)//是同一个tbody时,就将innerText放到剪切版
{
window.clipboardData.setData("Text", trElements[i].innerText);
}
}
}
function ParseCode()
{
document.all("txt").value = window.clipboardData.getData("Text");//粘贴
}
</script>
<table width="100%" cellspacing="0" cellpadding="0" name="q" id="q">
<tbody id="w">
<tr id="c">
<th id="d">C#?</th>
<th id="e">
<span id="a" class="copyCode" onclick="CopyCode(this)" tabindex="0">
<img class="copyCodeImage" name="ccImage" align="absmiddle" alt="CopyCode image" src="../local/copycode.gif"></img>复制代码
</span>
</th>
</tr>
<tr id="f">
<td colspan="2" id="g">
<pre>
public partial class Employee
{
public void DoWork()
{
}
}
public partial class Employee
{
public void GoToLunch()
{
}
}
</pre>
</td>
</tr>
</tbody>
<tbody>
<tr>
<textarea rows="10" cols="50" id="txt"></textarea><input onClick="javascript:ParseCode();" type="button" value="粘帖" id="cmdParse">
</tr>
</tbody>
</table>
</body>
</html>
lei1217