gpt4 book ai didi

javascript - jsp-如何在 JavaScript 函数中复制 URL?

转载 作者:行者123 更新时间:2023-12-02 23:59:34 26 4
gpt4 key购买 nike

我有一个如下所示的 JSP 页面:

enter image description here

我添加了一个按钮复制 URL,不言自明的是,当我单击该按钮时,我需要复制该特定链接本身的唯一 URL。

这就是我的前端的样子:

<%
//need to input logic to populate data on each row

int counter=0;

String[] split = request.getParameter("nodeID").split(",",0);
for(int i=0;i<split.length;i++){

long file=Long.parseLong(split[i]);

List files = fileFacade.list_items(file);
for (Iterator rstltr = files.iterator(); rstltr.hasNext();) {
Fmedia fv = (Fmedia) rstltr.next();
Node nd = nodeFacade.get(fv.getNodeid(), false);
// Fmedia fm = fileFacade.get_file(fv.getNodeid());

int count = 0;
count++;
long fileid= nd.getNodeid();



%>


<tbody>
<tr>
<td width="5%">
<!--Display Checkbox -->
<input type="checkbox" name="name1" />&nbsp;
</td>

<td>
<!--Display No -->
<% counter=counter+1;
out.print(counter);

%>


</td>
<td width="28%">

<!-- Display Filename -->
<%=nd.getNodedesc()%>



</td>
<td width="100%">
<!-- Display URL -->


<%="http://localhost:8080/repository/file/view/viewPDF.jsp?count=1&f0="+nd.getNodeid()%>
<%

fileFacade.insert_url(nd.getNodeid(),"http://localhost:8080/repository/file/view/viewPDF.jsp?count=1&f0="+nd.getNodeid());


%>


</td>
<td>
<!-- Display EDIT/DEL -->

</td>
<td> <!-- Display COPY feature -->

<input type="button" value="Copy URL" onclick="msg()">
<script>
function msg() {
alert("Hello world!");
}
</script>
</td>


</tr>

</tbody>


<%}}
%>

我需要一种方法,让每个按钮独立于每一行,并需要一个 JavaScript 函数,以便能够在选择复选框后复制链接。

我不太确定如何从这里继续。

谁能提出一个好的方法吗?

编辑:

Javascript函数:

<input type="button" value="Copy URL" onclick="getURL()">
<script>
function getURL() {
var copyText = "http://localhost:8080/repository/file/view/viewPDF.jsp?count=1&f0="+<%nd.getNodeid();%>
var el = document.createElement('textarea');
el.value = copyText;
el.setAttribute('readonly', '');
el.style = {
position: 'absolute',
left: '-9999px'
};
document.body.appendChild(el);
el.select();
document.execCommand('copy');
document.body.removeChild(el);
}
</script>
</td>

最佳答案

您必须将 URL 传递给函数,请遵循以下代码

<tbody>
<tr>
<td width="5%">
<!--Display Checkbox -->
<input type="checkbox" name="name1" />&nbsp;
</td>

<td>
<!--Display No -->
<% counter=counter+1;
out.print(counter);

%>


</td>
<td width="28%">

<!-- Display Filename -->
<%=nd.getNodedesc()%>



</td>
<td width="100%">
<!-- Display URL -->


<%="http://localhost:8080/repository/file/view/viewPDF.jsp?count=1&f0="+nd.getNodeid()%>
<%

fileFacade.insert_url(nd.getNodeid(),"http://localhost:8080/repository/file/view/viewPDF.jsp?count=1&f0="+nd.getNodeid());


%>


</td>
<td>
<!-- Display EDIT/DEL -->

</td>
<td> <!-- Display COPY feature -->

<input type="button" value="Copy URL" onclick="copyURL('<%="http://localhost:8080/repository/file/view/viewPDF.jsp?count=1&f0="+nd.getNodeid()%>')">

</td>


</tr>

</tbody>


<%}}
%>

<script>
function copyURL(url) {
var copyText = url;
var el = document.createElement('textarea');
el.value = copyText;
el.setAttribute('readonly', '');
el.style = {
position: 'absolute',
left: '-9999px'
};
document.body.appendChild(el);
el.select();
document.execCommand('copy');
document.body.removeChild(el);
}
</script>

关于javascript - jsp-如何在 JavaScript 函数中复制 URL?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55235259/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com