Javascript - innerHTML & 的使用代码审查中的 id 方法
所有,
这是我正在编写的脚本的代码。
它的作用: 当按下蓝色按钮时,包含“5000”数字的框顶部应出现一个 div。
我遇到的问题是使用innerHTML &为动态创建的 div 分配一个 id。我在 Firebug 中没有收到任何错误,所以我不确定我出了什么问题/哪里出了问题。
问题行:
eCreditTransactions[i].id = ("trans" + i);
eCreditTransactions[i].innerHTML = '<div class="cCreditContainer"><span class="cCreditsNo">-50</span> <img class="cCurrency" src="" alt="" /></div><span class="cCloseMsg">Click box to close.</span><div class="dots"></div><div class="dots"></div><div class="dots"></div>';
这是完整的代码:
var eCreditSystem = document.getElementById("creditSystem");
var i = 0;
var eCreditTransactions = new Array(6); // 6 instances created which will be recycled
function createCreditTransaction () // func called when a transaction occurs, at the mo, attached to onclick()
{
if (i < 6)
{
$(eCreditTransactions[i]).remove();
eCreditTransactions[i] = undefined; // to delete the existing data in the index of array
addingElements (i); // calling function
} else
if (i > 5 || eCreditTransactions[i] != undefined)
{
i = 0;
$(eCreditTransactions[i]).remove();
eCreditTransactions[i] = undefined;
console.log(eCreditTransactions[i]);
addingElements (i);
}
}
function addingElements (arrayIndex) // func called from within the 'createCreditTransaction()' func
{
console.log(eCreditTransactions[i]);
eCreditTransactions[i] = $(document.createElement('div')).addClass("cCreditTransaction").appendTo(eCreditSystem);
// the problem area
eCreditTransactions[i].id = ("trans" + i);
eCreditTransactions[i].innerHTML = '<div class="cCreditContainer"><span class="cCreditsNo">-50</span> <img class="cCurrency" src="" alt="" /></div><span class="cCloseMsg">Click box to close.</span><div class="dots"></div><div class="dots"></div><div class="dots"></div>';
console.log(eCreditTransactions[i]);
return i++;
}
All,
Here is my code for a script I am working on.
WHAT IT DOES:
When the blue button is pressed, a div should appear on top of the box containing the '5000' number.
The problem I am having is with the usage of innerHTML & assigning an id to dynamically created div. I am not getting any errors in Firebug so am not sure what/ where I am going wrong.
The problem lines:
eCreditTransactions[i].id = ("trans" + i);
eCreditTransactions[i].innerHTML = '<div class="cCreditContainer"><span class="cCreditsNo">-50</span> <img class="cCurrency" src="" alt="" /></div><span class="cCloseMsg">Click box to close.</span><div class="dots"></div><div class="dots"></div><div class="dots"></div>';
And here is the full code:
var eCreditSystem = document.getElementById("creditSystem");
var i = 0;
var eCreditTransactions = new Array(6); // 6 instances created which will be recycled
function createCreditTransaction () // func called when a transaction occurs, at the mo, attached to onclick()
{
if (i < 6)
{
$(eCreditTransactions[i]).remove();
eCreditTransactions[i] = undefined; // to delete the existing data in the index of array
addingElements (i); // calling function
} else
if (i > 5 || eCreditTransactions[i] != undefined)
{
i = 0;
$(eCreditTransactions[i]).remove();
eCreditTransactions[i] = undefined;
console.log(eCreditTransactions[i]);
addingElements (i);
}
}
function addingElements (arrayIndex) // func called from within the 'createCreditTransaction()' func
{
console.log(eCreditTransactions[i]);
eCreditTransactions[i] = $(document.createElement('div')).addClass("cCreditTransaction").appendTo(eCreditSystem);
// the problem area
eCreditTransactions[i].id = ("trans" + i);
eCreditTransactions[i].innerHTML = '<div class="cCreditContainer"><span class="cCreditsNo">-50</span> <img class="cCurrency" src="" alt="" /></div><span class="cCloseMsg">Click box to close.</span><div class="dots"></div><div class="dots"></div><div class="dots"></div>';
console.log(eCreditTransactions[i]);
return i++;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在这行代码中:
您正在创建一个 dom 元素,然后将其包装在 jQuery 对象 中
以访问该对象上的 id 属性,您需要使用其方法,
相同
与innerHTML属性使用jQuery方法 html();
请参阅此处文档
in this line of your code:
you are creating a dom element and then wrapping it in a jQuery object
to access an id attribute on that object you need to use its methods
the same for innerHTML property
use jQuery method html();
see here docu