我正在尝试使用用户通过JavaScript警告框输入的信息在HTML页面上创建链接

时间:2013-09-04 02:57:47

标签: javascript html

基本上我到目前为止是一个有两个弹出/警告框的页面。第一个要求用户输入他们喜欢的网站的名称。第二个要求用户将URL输入到他们喜欢的网站。结果应该显示在主页上的超链接中,作为用户输入的名称,该名称指向他们在单击时输入的URL。这是我的代码:

<!DOCTYPE HTML>
<html>
    <head>
        <title>Untitled</title>
    </head>
    <body>
        <script type="text/javascript">
            var favoriteSite;
            favoriteSite = prompt ("What is your favorite web site?")
            favoriteSite = prompt ("What is the URL of that site?") 
            document.write('<a href="' + favoriteSite + '"></a>')
        </script>                       
        <h1>Link to favorite site.</h1>
        <h2>This is my favorite web site</h2>  
    </body>
</html>

PS。我是JavaScript的初学者,任何帮助将不胜感激。感谢。

这是我最终改为:

<html>
    <head>
        <title>Untitled</title>
    </head>
    <body>
        <script type="text/javascript">
            var favoriteSite;
            var favoriteSiteName;
            favoriteSiteName = prompt ("What is your favorite web site?");
            favoriteSite = prompt ("What is the URL of that site?"); 
            document.write("<h1>Link to favorite website.</h1>");
            document.write("<h2>This is my favorite web site" +" " +     favoriteSiteName.link("https://" + favoriteSite + "") + "</p>");
        </script>                       
    </body>
</html>

6 个答案:

答案 0 :(得分:0)

你所拥有的几乎正确。

var favoriteSiteName = '',
    favoriteSiteURL = '';

favoriteSiteName = prompt('What is your favorite web site?');
favoriteSiteURL = prompt('What is the URL of that site?');
document.write('<a href="' + favoriteSiteURL + '">' + favoriteSiteName + '</a>');

您缺少分号以指示行的结尾并将站点名称添加到链接。 此外,h1h2元素不属于script元素。

JSFiddle.

答案 1 :(得分:0)

您需要设置超链接的文本,否则超链接将不可见。

favoriteSiteName = prompt ("What is your favorite web site?")

favoriteSite = prompt ("What is the URL of that site?") 

document.write('<a href="' + favoriteSite + '">'+favoriteSiteName+'</a>')

在旁注中,您应该避免使用提示或警告。多个这样的对话框会惹恼用户,浏览器会发出警告,UI自动化测试变得困难。 您可以使用jquery ui对话框或div弹出窗口来获取用户输入并使用jquery来创建超链接。

答案 2 :(得分:0)

您缺少半冒号,并且网站和网址变量也相同。设置超链接的文本否则超链接将不可见     

var favoriteSite;
var favoriteUrl ;

 favoriteSite = prompt ("What is your favorite web site?");

 favoriteUrl = prompt ("What is the URL of that site?"); 

document.write('<a href="' + favoriteSite + '">Site</a>');
document.write('<a href="' + favoriteUrl + '">Url</a>');

</script>     

答案 3 :(得分:0)

远离document.write !!!

Why is document.write considered a "bad practice"?

Live Demo

<强> JS

//Get the text you want to display
var text = document.createTextNode(prompt ("What is your favorite web site?")); 

//Create an attribute to for the href
var href = document.createAttribute("href");
href.value = prompt ("What is the URL of that site?") 

//Create a link
var link = document.createElement('a');

//Add the attribute to the link
link.setAttributeNode(href);

//Add the text to the link
link.appendChild(text); 

//Add the link to the page
document.getElementById('link').appendChild(link);  

<强> HTML

<h1>Link to favorite site.</h1>

<h2>This is my favorite web site: <span id='link'></span></h2>  

最后,我会认真考虑使用框架来处理DOM操作。一个值得学习的好人是jQuery

答案 4 :(得分:0)

是的document.write会写入所有现有的文档节点..你应该尝试在运行脚本之前等待页面加载..试试这个

<!doctype html>

<html>
  <head>
    <script type='text/javascript'>
      window.addEventListener("load",function(){
        var siteName = prompt("what is you fave site name?");
        var siteURL = prompt("what is its URL?");
        var a = document.body.appendChild(document.createElement("a"));
        a.href = siteURL;
        a.innerHTML = siteName;
      });
    </script>
  </head>

  <body>

  </body>
</html>

答案 5 :(得分:0)

您的链接中没有任何内容。尝试这样的事情:

看JS:

http://jsbin.com/enOWeCI/1/edit

看到实际行动:

http://jsbin.com/enOWeCI/1/