最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to get ID value of first element with specific class in html by jQuery - Stack Overflow

programmeradmin4浏览0评论

I have this HTML code:

    <div id="calc_pol_list">
       <li class="calc_li">
           <span class="right_list_number">1</span>
           <p id="120" class="calc_p"/>
       </li>
       <li class="calc_li">
           <span class="right_list_number">2</span>
           <p id="100" class="calc_p"/>
       </li>
    </div>

I need to get ID of first p: with jQuery and convert this ID to string.

I've tried this code:

    $('#calc_pol_list p:first').attr('id');

But it doesn't works.

How can I do this?

UPDATE: Here is my function:

function refreshCost(){
    if ( jQuery.isReady ) {
        stomCost = parseInt($('div#calc_pol_list calc_p:first').attr('id'));
        var cost = stomCost + scatCost + polCost;               
        $("#pre_cost").text(cost);
    };
};

Yes, my code is correct, if considered separately from the rest of the code. I realized what the problem is, but I do not know how to solve it. The problem is that these

I add after the Ajax request that is not in their original DOM. The form, which owns these

is inside the div. I read here that I need to make the form a direct child of the body, but in this case it's impossible.

I have this HTML code:

    <div id="calc_pol_list">
       <li class="calc_li">
           <span class="right_list_number">1</span>
           <p id="120" class="calc_p"/>
       </li>
       <li class="calc_li">
           <span class="right_list_number">2</span>
           <p id="100" class="calc_p"/>
       </li>
    </div>

I need to get ID of first p: with jQuery and convert this ID to string.

I've tried this code:

    $('#calc_pol_list p:first').attr('id');

But it doesn't works.

How can I do this?

UPDATE: Here is my function:

function refreshCost(){
    if ( jQuery.isReady ) {
        stomCost = parseInt($('div#calc_pol_list calc_p:first').attr('id'));
        var cost = stomCost + scatCost + polCost;               
        $("#pre_cost").text(cost);
    };
};

Yes, my code is correct, if considered separately from the rest of the code. I realized what the problem is, but I do not know how to solve it. The problem is that these

I add after the Ajax request that is not in their original DOM. The form, which owns these

is inside the div. I read here that I need to make the form a direct child of the body, but in this case it's impossible.

Share Improve this question edited Oct 9, 2013 at 10:13 Shadow_ asked Oct 8, 2013 at 12:46 Shadow_Shadow_ 1601 gold badge1 silver badge14 bronze badges 7
  • 1 it should work, if it is executed after dom ready working - not working – Arun P Johny Commented Oct 8, 2013 at 12:49
  • 1 your code seems to be working fine.. – Nibin Commented Oct 8, 2013 at 12:49
  • 1 works ok here: jsfiddle/BfUbV – 97ldave Commented Oct 8, 2013 at 12:49
  • 1 id attribute should not begin with a number " ID and NAME tokens must begin with a letter ([A-Za-z]) and may be followed by any number of letters, digits ([0-9]), hyphens ("-"), underscores ("_"), colons (":"), and periods (".")" source – Stphane Commented Oct 8, 2013 at 13:00
  • Yes, my code is correct, if considered separately from the rest of the code. I realized what the problem is, but I do not know how to solve it. The problem is that these <p> I add after the Ajax request that is not in their original DOM. The form, which owns these <p> is inside the div. I read here that I need to make the form a direct child of the body, but in this case it's impossible. – Shadow_ Commented Oct 9, 2013 at 10:05
 |  Show 2 more ments

6 Answers 6

Reset to default 5
$('#calc_pol_list p').first().attr('id')

like this :)

What about this:

$('#calc_pol_list p').eq(0).attr('id');

jsfiddle http://jsfiddle/krasimir/ppzqx/1/

Solution is to use .ajaxStop

$(document).ajaxStop(function() {
    alert($('#calc_pol_list p').eq(0).attr('id'));
});

Try this:

$(document).ready(function(){ 
alert($('#calc_pol_list p:first').attr('id'));
});

ID and NAME tokens must begin with a letter ([A-Za-z]) and may be followed by any number of letters, digits ([0-9]), hyphens ("-"), underscores ("_"), colons (":"), and periods (".").

See: What are valid values for the id attribute in HTML?

Create a variable and get variable in it e.g.

var content = $('#calc_pol_list > li p:first').attr('id');

and alert it you will get id as string e.g.

alert (content );
发布评论

评论列表(0)

  1. 暂无评论