[英]Handling popup window with javascript
我有一個帶有“添加鏈接”按鈕的基本頁面,單擊后會彈出一個窗口。 彈出窗口具有一個用於輸入鏈接的表單字段。 輸入鏈接后,我想刷新基礎頁面-該基礎頁面不應再為“添加鏈接”,而應更改為在彈出窗口中輸入的超鏈接。
我是Java和HTML的新手。 到目前為止,我已經設法在基礎頁面上創建了一個按鈕,並在單擊時顯示了一個彈出窗口,其中包含鏈接的表單字段。 但是,我無法使用新鏈接刷新基本頁面。
下面是我的代碼:
<!DOCTYPE html>
<html>
<body>
<button onclick="myFunction()">Add Link</button>
<p id="demo"></p>
<script>
function myFunction()
{
prompt("Please Enter the Link");
}
</script>
</body>
</html>
嘗試這個:
<button onclick="myFunction()">Add Link</button>
<p id="demo"></p>
<script>
function myFunction()
{
var url = prompt("Please Enter the Link");
window.location = url;
}
</script>
http://jsfiddle.net/douglasloyo/rtghQ/ js小提琴可接受的唯一值將是http://jsfiddle.net/
干杯
以下是每次單擊按鈕時都會顯示的提示,然后在提示關閉后,腳本將向demo
div添加一個新的<a href=value>New Link</a>
標簽。
<!DOCTYPE html>
<html>
<body>
<button onclick="myFunction()">Add Link</button>
// This needs to be a 'div' so it can have elements inside it.
<div id="demo"></div>
<script>
var demo = document.getElementById('demo');
function myFunction()
{
// Stores the value from the prompt() in the variable 'value'
var value = prompt("Please Enter the Link");
var link = document.createElement('a'); // Create an <a> tag
link.setAttribute('href', value); // Set the link's URL to the value.
link.innerHTML = "New Link"; // Set the link's display text.
demo.appendChild(link); // Add the link to the demo div.
}
</script>
</body>
</html>
希望這些評論能解釋它是如何工作的,如果不只是在這里問的話:)
您將需要某種方式將鏈接存儲在服務器上。 刷新頁面將導致所有內容重新執行,並且您不能真正動態地更改自身的html文件。 帶有PostgreSQL數據庫的django之類的東西很容易設置,但這超出了您的問題范圍。
也就是說,如果您可以在刷新頁面時保持鏈接不存在,則可以使用文本區域代替提示。 如果您使用jQuery,請參見以下示例:
<head>
<script>
$(function() {
$("#addLink").click(function() {
var linkStr = document.getElementById("linkInput").value;
$("#linkArea").html("<a href=" + linkStr + ">Your link</a>");
});
})
</script>
</head>
<span id="linkArea">
<input id="addLink" type="submit" value="Add Link: ">
<input id="linkInput" type="text" value="(enter link)">
</span>
這是一個可以嘗試的jsfiddle: http : //jsfiddle.net/tRcUp/
哦,通常,您應該將腳本放在head標簽中。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.