English 中文(简体)
• 如何获得使用j Query的 element子
原标题:How to get the id of an element ON PAGE LOAD using jQuery

我完全知道如何获得它喜欢的被点击元素:

$("button").click(function(){
console.log($(this).attr("id"));
}

但是,我能做些什么来把id放在网页上? 参看这一点。

$("button").ready(function(){
console.log($(this).attr("id"));
}

该文件全文和内容。

$("button").load(function(){
console.log($(this).attr("id"));
}

根本就没有。

我想积极改变所有纽托邦的风格。

主要项目更为复杂,我不想用碎块来做,但我想简明扼要地挑选 j子,但同样的做法受到赞赏。

最佳回答

i 想动态地改变所有装货吨的风格。

如果情况如此,你可以简单地将<代码>cs应用于button 装载时的选定器,而无需填写id

$(function() {
    $("button").css("background-color", "#C00");
});

更糟糕的是,将捆绑成一个等级,并对所有县适用一个等级:

$(function() {
    $("button").addClass("red-bg");
});

如果你确实希望每个纽扣吨的<代码>id,那么你需要使用一个阵列来满足以下事实:

var buttonIds = $("button").map(function() {
    return this.id;
}).get();

然而,这是一种相当无意义的方法,因为你只能使用<条码>/条码>,在<条码>丁顿/条码>上检索每一条异构体。

问题回答

你们可以把所有纽子元素带入 j,然后把 each带上每一纽子元素,改变其风格:

$(function()
{
    var allButtons = $( button );
    $.each(allButtons,function(index,element)
    {
        $(element).css( width , 100px );
    });
});

我认为,你重新审视了这一问题:

        $(document).ready(function () {
            $(":button").each(function () {
                console.log($(this).attr("id"));
            })
        });




相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签