Javascript - innerHTML & 的使用代码审查中的 id 方法

发布于 2024-12-18 07:56:18 字数 2315 浏览 2 评论 0原文

所有,

这是我正在编写的脚本的代码。

它的作用: 当按下蓝色按钮时,包含“5000”数字的框顶部应出现一个 div。

我遇到的问题是使用innerHTML &为动态创建的 div 分配一个 id。我在 Firebug 中没有收到任何错误,所以我不确定我出了什么问题/哪里出了问题。

问题行:

eCreditTransactions[i].id = ("trans" + i);

eCreditTransactions[i].innerHTML = '<div class="cCreditContainer"><span class="cCreditsNo">-50</span>&nbsp;<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>&nbsp;<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++;
}

Jsfiddle

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++;
}

Jsfiddle

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

浅语花开 2024-12-25 07:56:18

在这行代码中:

 eCreditTransactions[i] = $(document.createElement('div'))

您正在创建一个 dom 元素,然后将其包装在 jQuery 对象

以访问该对象上的 id 属性,您需要使用其方法,

 eCreditTransactions[i].attr('id', 'newId');

相同

与innerHTML属性使用jQuery方法 html();

 eCreditTransactions[i].html(yourHtmlString);

请参阅此处文档

in this line of your code:

 eCreditTransactions[i] = $(document.createElement('div'))

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

 eCreditTransactions[i].attr('id', 'newId');

the same for innerHTML property

use jQuery method html();

 eCreditTransactions[i].html(yourHtmlString);

see here docu

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文