2017-05-24 41 views
0

我需要添加具有固定大小,固定背景色不透明度和可變背景色的html(不一定需要是跨度)。我嘗試這樣做:使用嵌套引號構建字符串

var tr = htmlDocument.getElementById(myId).tBodies[0].children; 
tr[1].children[5].innerHTML = '<center><span ng-style="{'background-color': getColor(parseFloat(myArray[index]))}">' + myArray[index] + '</span></center>'; 

和我:

Uncaught SyntaxError: Unexpected identifier 

的編號工作完全正常,所以不介意。我也不知道如何添加不透明度(比如說50%)。

+0

使用與innerHTML的角屬性? – epascarello

+0

你的問題是你在單引號裏面有單引號的事實.... – epascarello

+0

我是一個完整的noob我不知道這是什麼意思 – defoification

回答

0

逃生內單引號,並給它一個樣式屬性:

tr[1].children[5].innerHTML = '<center><span style="opacity: 50%; font-size: 15px" ng-style="{\'background-color\': getColor(parseFloat(myArray[index]))}">' + myArray[index] + '</span></center>'; 
+0

啊,是的逃脫。我會試試這個,但是在我做之前你可以加入50%的不透明度? – defoification

+0

和大小15或其他 – defoification

+0

我看不到顏色 – defoification

0

的另一種方法,有很多清潔:

var tr = htmlDocument.getElementById(myId).tBodies[0].children; 

var ngStyle = { 
    "background-color": getColor(parseFloat(myArray[index])) 
}; 
var span = document.createElement("span"); 
span.setAttribute("ng-style", JSON.stringify(ngStyle)); 
span.innerHTML = myArray[index]; 

tr[1].children[5].innerHTML = ""; 
tr[1].children[5].insertBefore(span, null); 
tr[1].children[5].style.textAlign = "center"; 
tr[i].children[5].style.opacity = 0.5; 
+0

我沒有看到顏色,而是將它插入已存在的跨度旁邊。我想替換那個位置的跨度。 – defoification

+0

添加一行可以在插入跨度之前清除單元格。至於顏色,這是'ng-style'提供的,所以如果你沒有看到它,你需要檢查你的其他代碼。 –