我正在使用 Bootstrap 来设置我的网页样式,它几乎可以完美运行,但是当我尝试使用 $.append() 将新的 html 添加到页面时。那么新的 html 不会获得默认的引导样式。这是我的相关html:
<div id="lines">
<div id="line1">
<input type="tel" class="Numbers" id="Number1" />
<input type="tel" class="Numbers" id="Number2" />
<input type="tel" class="Numbers" id="Number3" />
<input type="tel" class="Numbers" id="Number4" />
<input type="tel" class="Numbers" id="Number5" />
<input type="tel" class="Numbers" id="Number6" />
<input type="tel" class="Numbers" id="StrongNumber1" style="background-color:cyan"/>
<button class="btn btn-primary addbtns" id="btnAdd1" onclick="addLine()">+</button>
<button class="btn btn-primary addbtns" id="btnMinus1" disabled="disabled">-</button>
</div>
</div>
这是我的 JQuery:
function addLine() {
$('#btnAdd' + lineCounter).attr('disabled', 'true');
$('#btnMinus' + lineCounter).attr('disabled', 'true');
++lineCounter;
var line = "<div id='line" + lineCounter + "'>" +
"<input type='tel' class='Numbers' id='Number" + lineCounter + "' />" +
"<input type='tel' class='Numbers' id='Number" + lineCounter + "' />" +
"<input type='tel' class='Numbers' id='Number" + lineCounter + "' />" +
"<input type='tel' class='Numbers' id='Number" + lineCounter + "' />" +
"<input type='tel' class='Numbers' id='Number" + lineCounter + "' />" +
"<input type='tel' class='Numbers' id='Number" + lineCounter + "' />" +
"<input type='tel' class='Numbers StrongNumber' id='StrongNumber" + lineCounter + "' style='background-color:cyan' />" +
"<button class='btn btn-primary addbtns' id='btnAdd" + lineCounter + "' onclick='addLine()'>+</button>" +
"<button class='btn btn-primary addbtns' id='btnMinus" + lineCounter + "' onclick='removeLine()'>-</button>" +
"</div>";
$('#lines').append(line).fadeIn('slow');
}
编辑:好的,我试图用最简单的页面和一个简单的附加来重现这个错误。还像@Sherbrow 建议的那样将 JS ' 切换为 " ,但它仍然无法正常工作..
这是我的 HTML:
<html xmlns="http://www.w3.org/1999/xhtml">
<link href="http://twitter.github.com/bootstrap/assets/css/bootstrap.css" rel="stylesheet">
<link href="test.css" rel="stylesheet"/>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<script src="test.js"></script>
<head></head>
<body dir="rtl">
<div id="lines">
<div id="line1">
<input type="tel" class="Numbers" id="Number1" />
<input type="tel" class="Numbers" id="Number2" />
<input type="tel" class="Numbers" id="Number3" />
<input type="tel" class="Numbers" id="Number4" />
<input type="tel" class="Numbers" id="Number5" />
<input type="tel" class="Numbers" id="Number6" />
<input type="tel" class="Numbers" id="StrongNumber1" style="background-color:cyan"/>
<button class="btn btn-primary addbtns" id="btnAdd1" onclick="addLine()">+</button>
<button class="btn btn-primary addbtns" id="btnMinus1" disabled="disabled">-</button>
</div>
</div>
</body>
</html>
我的CSS:
body
{
background-color:#E46C0B;
text-align:center;
}
.Numbers
{
width: 16px;
}
我的 JavaScript:
var lineCounter=1;
function addLine() {
$('#btnAdd' + lineCounter).attr('disabled', 'true');
$('#btnMinus' + lineCounter).attr('disabled', 'true');
++lineCounter;
var line = '<div id="line' + lineCounter + '">' +
'<input type="tel" class="Numbers" id="Number' + lineCounter + '1" />' +
'<input type="tel" class="Numbers" id="Number' + lineCounter + '2" />' +
'<input type="tel" class="Numbers" id="Number' + lineCounter + '3" />' +
'<input type="tel" class="Numbers" id="Number' + lineCounter + '4" />' +
'<input type="tel" class="Numbers" id="Number' + lineCounter + '5" />' +
'<input type="tel" class="Numbers" id="Number' + lineCounter + '6" />' +
'<input type="tel" class="Numbers StrongNumber" id="StrongNumber"' + lineCounter + '" style="background-color:cyan" />' +
'<button class="btn btn-primary addbtns" id="btnAdd"' + lineCounter + '" onclick="addLine()">+</button>' +
'<button class="btn btn-primary addbtns" id="btnMinus"' + lineCounter + '" onclick="removeLine()">-</button>' +
'</div>';
$('#lines').append(line);
}
我究竟做错了什么?
感谢您的回答:)