2016-10-14 95 views
0

我想用document.write替換innerHTML。但是當我嘗試時它不會創建鏈接。我不知道我是如何構建路徑變量的問題,因此它不會將鏈接正確打印出來。現在它不會打印出任何鏈接。如何用innerHTML替換document.write

<!DOCTYPE html> 
<html lang="en"> 

<script> 

    var path = ""; 
    var href = document.location.href; 
    var s = href.split("/"); 
    for (var i=2;i<(s.length-1);i++) { 
     path+="<a class='crumb' href=\""+href.substring(0,href.indexOf("/"+s[i])+s[i].length+1)+"/\">"+s[i]+" </a>"; 
    } 
    i=s.length-1; 
    path+="<a class='crumb' href=\""+href.substring(0,href.indexOf(s[i])+s[i].length)+"\">"+s[i]+" </a>"; 
    var url = path; 
    //document.getElementById(".breadcrumb").innerHTML = url; 
    document.write(url); 

</script> 

<head> 
<meta charset="utf-8"> 
<meta http-equiv="X-UA-Compatible" content="IE=edge"> 
<meta name="viewport" content="width=device-width, initial-scale=1"> 
<link rel="stylesheet" href="/webjars/bootstrap/3.3.6/css/bootstrap.min.css"> 
<link rel="stylesheet" href="/webjars/bootstrap-fileinput/4.2.7/css/fileinput.min.css"> 
<link rel="stylesheet" href="/webjars/bootswatch/3.3.5+4/yeti/bootstrap.min.css"> 
<link rel="stylesheet" href="/webjars/font-awesome/4.5.0/css/font-awesome.min.css"> 
<link rel="stylesheet" href="/webjars/jquery-ui/1.12.1/jquery-ui.min.css"> 
<link rel="stylesheet" href="/static/app/css/app.css"> 
<link rel="stylesheet" href="/static/app/css/style.css"> 
<script src="/webjars/jquery/2.1.4/jquery.min.js"></script> 
<script src="/webjars/jquery-ui/1.12.1/jquery-ui.min.js"></script> 
<script src="/webjars/bootstrap-fileinput/4.2.7/js/fileinput.js"></script> 
<script src="/webjars/bootstrap/3.3.6/js/bootstrap.min.js"></script> 
<script src="/webjars/bootstrap-fileinput/4.2.7/js/fileinput_locale_ja.js"></script> 
<script src="/static/app/js/app.js"></script> 

<style> 

.breadcrumb 
{ 
    witdh : 100%; 
    text-align: center; 
    border: 5px solid transparent; 
} 

.crumb 
{ 
display: inline-block; 
float : left; 
font: 16px Helvetica, Arial, Sans-Serif; 
color: white; 
background-color: #4E95B6; 
border: 1px solid black; 
} 
.crumb: hover 
{ 
color: red; 
} 

</style> 

</head> 

<body> 

{{>partials/browser-compatibility}} 
<div class = "breadcrumb"> </div> 
<div class="container-fluid"> 
+0

請編輯您的問題,以便爲包括[最小,完整,可驗證的示例](http://stackoverflow.com/help/mcve )。 – MJH

+0

這不能工作'document.getElementById('。breadcrumb')' – antoni

+0

你的腳本標籤位於html中?顯示html –

回答

4

你可以嘗試document.getElementsByClassName('breadcrumb')[0].innerHTML = url;

+0

我也試過這種方式 – MNM

+0

只需將

2

使用document.ready確保已加載dom;

你錯誤地使用了getElementById;

<div id ="breadcrumb"> </div> 
 
<script> 
 

 
    var path = ""; 
 
    var href = document.location.href; 
 
    var s = href.split("/"); 
 
    for (var i=2;i<(s.length-1);i++) { 
 
     path+="<a class='crumb' href=\""+href.substring(0,href.indexOf("/"+s[i])+s[i].length+1)+"/\">"+s[i]+" </a>"; 
 
    } 
 
    i=s.length-1; 
 
    path+="<a class='crumb' href=\""+href.substring(0,href.indexOf(s[i])+s[i].length)+"\">"+s[i]+" </a>"; 
 
    var url = path; 
 
    document.getElementById('breadcrumb').innerHTML = url; 
 
    //document.write(url); 
 
    </script>