Javascript检查'URL'是空的,然后填充“”,否则显示url作为超链接

Javascript检查'URL'是空的,然后填充“”,否则显示url作为超链接

问题描述:

我有一些Javascript显示来自XML的标题。Javascript检查'URL'是空的,然后填充“”,否则显示url作为超链接

当标题被点击时,与其相关的信息会弹出。当URL为空时,我遇到问题。 如果它是空的,我不想显示URL。它应该看起来像URL。

如果存在,我希望URL显示为超链接。

我是新来的,所以请对我轻松点!

XML

<?xml version="1.0"?> 
<notepad> 
    <nextID>114</nextID> 
    <memos> 
    <memo id="100"> 
     <title>Valentines</title> 
     <sender>James Mcavoy</sender> 
     <recipient>Clara Croft</recipient> 
     <date>11/09/17</date> 
     <message>Let me know what you would like for Valentines</message> 
     <url>www.heresyourpresent.com</url> 
    </memo> 
    <memo id="101"> 
     <title>Birthday</title> 
     <sender>Louise King</sender> 
     <recipient>Lily May</recipient> 
     <date>13/10/17</date> 
     <message>I will be there for your birthday! What cake would you like?</message> 
     <url>www.findmycake.com</url> 
    </memo> 
    <memo id="102"> 
     <title>Christmas</title> 
     <sender>Monty Python</sender> 
     <recipient>Jack Maloe</recipient> 
     <date>16/11/17</date> 
     <message>What would you like for Chrismtas Jack ?</message> 
     <url>www.socksrus.co.uk</url> 
    </memo> 
    <memo id="103"> 
     <title>Easter</title> 
     <sender>Carolyn Lynemouth</sender> 
     <recipient>Mercedes Hugh</recipient> 
     <date>30/04/17</date> 
     <message>I got a few easter eggs for your daughter on this site, is this ok?</message> 
     <url>www.eastereggs.com</url> 
    </memo> 
    <memo id="104"> 
     <title>Christening</title> 
     <sender>Dylan Bramble</sender> 
     <recipient>Kelly Love</recipient> 
     <date>07/05/17</date> 
     <message>I have sent you an e-voucher for a baby store</message> 
     <url>www.firstchristening.com</url> 
    </memo> 
    <memo id="105"> 
     <title>Wedding</title> 
     <sender>Polly Wisener</sender> 
     <recipient>Natasha Kirkpatrick</recipient> 
     <date>12/06/18</date> 
     <message>Remember to go to attend the dress fitting please on Friday</message> 
     <url>dgdfgdffg</url> 
    </memo> 
    <memo id="106"> 
     <title>Graduation</title> 
     <sender>Kathryn Kirke</sender> 
     <recipient>Cara Lovejoy</recipient> 
     <date>04/07/18</date> 
     <message>Just to let you know I can't make your graduation, sorry</message> 
     <url>www.makegraduationspecial.co.uk</url> 
    </memo> 
    <memo id="107"> 
     <title>Anniversary</title> 
     <sender>Eric Simpleton</sender> 
     <recipient>Grainne Simpleton</recipient> 
     <date>18/08/17</date> 
     <message>Be at le maison restaurant at 7pm this Thursday. I have a table booked</message> 
     <url>dfgdfgdfgfd</url> 
    </memo> 
    <memo id="108"> 
     <title>Baby Shower</title> 
     <sender>Hyacinth Bouquet</sender> 
     <recipient>Gemma Hamilton</recipient> 
     <date>20/12/17</date> 
     <message>I will attend your baby shower</message> 
     <url>www.babygift.co.uk</url> 
    </memo> 
    <memo id="109"> 
     <title>Stag</title> 
     <sender>John Smith</sender> 
     <recipient>Cara Lovejoy</recipient> 
     <date>22/12/17</date> 
     <message>Your stag do will be in Marbella. I have your tickets</message> 
     <url>www.thelaststag.co.uk</url> 
    </memo> 
    <memo id="110"> 
     <title>New Home</title> 
     <sender>Opal Newfridge</sender> 
     <recipient>Kylie Jenner</recipient> 
     <date>19/07/17</date> 
     <message>Congratulations on the new home. Can't wait for a cuppa</message> 
     <url>www.newhomesforyou.com</url> 
    </memo> 
    <memo id="111"> 
     <title>Communion</title> 
     <sender>John Smith</sender> 
     <recipient>Hilary Clinton, Jacob Marks, Lilly Popper</recipient> 
     <date>22/12/17</date> 
     <message>Your stag do will be in Marbella. I have your tickets</message> 
     <url>www.thelaststag.co.uk</url> 
    </memo> 
    <memo id="112"> 
     <title>Retirement Party</title> 
     <sender>Kylie Jenner</sender> 
     <recipient>Chloe Jenner, Rob Stark, Patrick Hunter, Julie Smith, Helen Doe</recipient> 
     <date>22/07/17</date> 
     <message>Please join us at Lodge Hotel for Robs Retirement Dinner</message> 
     <url>www.lodge.com</url> 
    </memo> 
    <memo id="113"> 
     <title>Fathers Day</title> 
     <sender>Carolyn Lynemouth</sender> 
     <recipient>Rob Stark</recipient> 
     <date>22/07/17</date> 
     <message>We will be sending you away for the weekend to London!</message> 
     <url>www.easyflights.com</url> 
    </memo> 
    </memos> 
</notepad> 

代码如下

xmlhttp = new XMLHttpRequest(); 
xmlhttp.open("GET", "memos.xml", false); 
xmlhttp.send(); 
xmlDocument = xmlhttp.responseXML; 
memos = xmlDocument.getElementsByTagName("memo"); 

table = "<tr><th>Title</th></tr>"; 
for (count = 0; count < memos.length; count++) { 
    table += "<tr onclick='displayTitle(" + count + ")'><td>"; 
    table += memos[count].getElementsByTagName("title")[0].childNodes[0].nodeValue; 
    table += "</td>"; 
} 
document.getElementById("demo").innerHTML = table; 

function displayTitle(count) { 

    document.getElementById("showTitle").innerHTML = 
    "ID: " + 
    memos[count].getAttribute("id") + 
    "<br>Title: " + 
    memos[count].getElementsByTagName("title")[0].childNodes[0].nodeValue + 
    "<br>Sender: " + 
    memos[count].getElementsByTagName("sender")[0].childNodes[0].nodeValue + 
    "<br>Recipient: " + 
    memos[count].getElementsByTagName("recipient")[0].childNodes[0].nodeValue + 
    "<br>Date: " + 
    memos[count].getElementsByTagName("date")[0].childNodes[0].nodeValue + 
    "<br>Message: " + 
    memos[count].getElementsByTagName("message")[0].childNodes[0].nodeValue + 
    "<br>Url: " + 
    urlNode = memos[count].getElementsByTagName("url")[0]; 
    if (urlNode.hasChildNodes()) { 
    url = urlNode.childNodes[0].nodeValue; 
    } else { 
    url = ""; 
    } 

} 
+0

当您发布的代码运行时会发生什么?一个问题应该总是说出你期望发生的事情,以及实际发生的事情。如果你发布了一个xml的例子,它也可以帮助回答者。 – stone

您试图设置您的网址 “” 你已经更新的innerHTML后?

xmlhttp = new XMLHttpRequest(); 
xmlhttp.open("GET", "memos.xml", false); 
xmlhttp.send(); 
xmlDocument = xmlhttp.responseXML; 
memos = xmlDocument.getElementsByTagName("memo"); 

table="<tr><th>Title</th></tr>"; 
for (count = 0; count <memos.length; count++) { 
    table += "<tr onclick='displayTitle(" + count + ")'><td>"; 
    table += memos[count].getElementsByTagName("title")[0].childNodes[0].nodeValue; 
    table += "</td>"; 
} 
document.getElementById("demo").innerHTML = table; 

function displayTitle(count) { 
    var urlNode = memos[count].getElementsByTagName("url")[0]; 
    var url = ""; 
    if (urlNode.hasChildNodes()) { 
     url = urlNode.childNodes[0].nodeValue; 
    } else { 
     url = ""; 
    } 

    document.getElementById("showTitle").innerHTML = 
    "ID: " + 
    memos[count].getAttribute("id")+ 
    "<br>Title: " + 
    memos[count].getElementsByTagName("title")[0].childNodes[0].nodeValue + 
    "<br>Sender: " + 
    memos[count].getElementsByTagName("sender")[0].childNodes[0].nodeValue + 
    "<br>Recipient: " + 
    memos[count].getElementsByTagName("recipient")[0].childNodes[0].nodeValue + 
    "<br>Date: " + 
    memos[count].getElementsByTagName("date")[0].childNodes[0].nodeValue + 
    "<br>Message: " + 
    memos[count].getElementsByTagName("message")[0].childNodes[0].nodeValue + 
    "<br>Url: " + url;  

} 
+0

我尝试了类似的东西,然后报废了!非常感谢你 –

+0

@P.Smith所以现在按预期工作?如果是这样,请接受我的文章作为答案。在将来在尝试在html中使用它们之前,先声明和赋值变量,这些都是绝对的基础。 – Wouter