2017-10-23 9 views
0

javascriptテーブルにURLが表示されている場合は、表示されます。Javascriptで表示されるハイパーリンクのURL

このURLは、ユーザーがクリックすることはできますが、取得できないハイパーリンクにしたいと考えています。

私は試してみましたが、それを正しく取得できませんし、動作させることができません。どんな助けもありがとう。

そのおそらく何か簡単ではあるが、私の頭は

<script type="text/javascript"> 
    //var memos; 

    if (window.XMLHttpRequest) { // IE7+, Firefox, Chrome, Opera, Safari 
     var xmlhttp = new XMLHttpRequest(); 
    } else { // IE6, IE5 
     var xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); 
    } 
    xmlhttp.open("GET", "memos.xml", false); 
    xmlhttp.send(); 
    xmlDoc = xmlhttp.responseXML; 
    memos = xmlDoc.getElementsByTagName("memo"); 

    // Function to get values from xml and print in a table 
    function showTable() { 
     // For loop to iterate through each memo in XML. 
     for (var count = 0; count < memos.length; count++) { 
      // Get attribute 'id' from xml and assign to id variable. 
      id = memos[count].getAttribute("id"); 
      // Get element title from XML and get its value and assign to 'title' variable. 
      title = memos[count].getElementsByTagName("title")[0].childNodes[0].nodeValue; 
      sender = memos[count].getElementsByTagName("sender")[0].childNodes[0].nodeValue; 
      recipient = memos[count].getElementsByTagName("recipient")[0].childNodes[0].nodeValue; 
      date = memos[count].getElementsByTagName("date")[0].childNodes[0].nodeValue; 
      message = memos[count].getElementsByTagName("message")[0].childNodes[0].nodeValue; 
      urlNode = memos[count].getElementsByTagName("url")[0]; 
      //Check if URL element is empty. If empty then populate with "" else print out the URL value 
      if (urlNode.hasChildNodes()) { 
       url = urlNode.childNodes[0].nodeValue; 
      } else { 
       url = ""; 
      } 
      // Print out the table 
      document.write("<tr><td>" + id + "</td> " + 
       " <td>" + title + "</td> " + 
       " <td class='center'>" + sender + "</td> " + 
       " <td class='center'>" + recipient + "</td> " + 
       " <td class='center'>" + date + "</td> " + 
       " <td class='center'>" + message + "</td> " + 
       " <td class='center'>" + url + "</td></tr>"); 
     } 
    } 
</script> 
</head> 

<body> 
    <h1>List of Memos!</h1> 
    <br> 

    <script type="text/javaScript"> 
     document.write(" 
     <h2 align='center'>There are currently " + memos.length + " memos altogether</h2>"); document.write(" 
     <br>"); 
    </script> 

    <table class="table2"> 
     <tr> 
      <th>ID</th> 
      <th>Title</th> 
      <th>Sender</th> 
      <th>Recipient(s)</th> 
      <th>Date</th> 
      <th>Message</th> 
      <th>URL</th> 
     </tr> 

     <!-- Javascript to display the table --> 
     <script type="text/javascript"> 
     <!-- Call the showTable function created in Javascript above --> 
      showTable(); 
     </script> 

    </table> 

</body> 

</html> 

答えて

0

変更になりました揚げされています

" <td class='center'>" + url + "</td></tr>"); 

処理するように更新空白のURL

" <td class='center'><a href='#" + url + "'>" + url + "</a></td></tr>"); 

変更:!

" <td class='center'>" + url + "</td></tr>"); 

" <td class='center'><a href='" + ((url == null || url == "") ? "#" : url) + "'>" + url + "</a></td></tr>"); 
+0

は私がstupi Dであることを知っていました。私はそれを変えようとしましたが、正しい組み合わせは決して得られませんでした。 –

+0

URLが空白の場合、 '' IOW:空白のアンカータグは、おそらく問題を引き起こすことはありませんが、認識すべきことがあります。 – Keith

+0

空のhrefを処理するために私の答えが更新されました。ありがとう! –

関連する問題