列出、计数元素,但属性不可用

ac1kyiln  于 2021-09-13  发布在  Java
关注(0)|答案(1)|浏览(351)

我可以列出所有元素并获得li元素的计数,但我无法获得元素的任何属性。
即使document.getelementbyid也不会返回任何内容。
有人知道为什么吗?我似乎不可能列出和计算项目,但无法获得任何属性。
脚本位于页面底部,并调用$().ready函数

$().ready(function () {
        $("*").each(function (i, e) {
            if (e == '[object HTMLLIElement]') {
                console.log(i + '-' + e);
            }
        });

        var docHeight = $(document).height();
        var docWidth = $(document).width();

        var teams = $('#teamsList').children().length
        var rungs = $('#ladderList').children().length
        var blankrungs = $('#ladderList').children('.blankrung').length
        var teamrungs = $('#ladderList').children('.teamrung').length
        var teamslistHeight = docHeight - 300;
        var teamslistWidth = teamslistHeight / 25 //console.log($('#teamsList li').first.height);
        var team42 = document.getElementById('#team42')

        // OK
        console.log('docHeight' + ' ' + docHeight)
        console.log('docWidth' + ' ' + docWidth)
        console.log('teams' + ' ' + teams)
        console.log('blankrungs' + ' ' + blankrungs)
        console.log('teamrungs' + ' ' + teamrungs)
        console.log('rungs' + ' ' + rungs)
        console.log('teamslistHeight' + ' ' + teamslistHeight)
        console.log('teamslistWidth' + ' ' + teamslistWidth)
        console.log($('#teamsList').children().first()) //lists all object properties
        console.log($('#teamsList'))//lists all object properties

        // do not work
        console.log($('#teamsList li').first.height);
        console.log(document.getElementById('#team42'))  //null
        console.log(team42)                              //null
        console.log('#team42' + ' ' + $('#team42').Text) //undefined
        //console.log('innerText' + ' ' + $('#teamsList').children().first().innerText()) // uncaught type error
        //console.log('team42' + ' ' + team42.Text)// uncaught type error
    });
'VB Razor
        <ul id="teamsList" class="connectedSortable flex-container-teams column">
            @For Each Rung As UserIrrationaListItem In (From i In Model.UserIrrationaListItems).Where(Function(w) w.PositionId = 0).OrderByDescending(Function(o) o.Colour1).ThenBy(Function(o) o.Colour2).ThenBy(Function(o) o.PositionId).ToList

                @<li id="@("team" + Rung.Id.ToString)" Class="sortable-item flex-item team" Style="background-color:@Rung.HexColour1; color:@Rung.HexColour2" ;
                   @Html.Raw(Rung.Name.ToString + " " + Rung.PositionId.ToString + "-" + Rung.RungId.ToString)
                </li>
            Next
        </ul>

为li元素创建的html

<li id="team42" class="sortable-item flex-item team ui-sortable-handle" style="background-color:#FFFFFF; color:#000000" ;>
Darlington 0-46
mrzz3bfm

mrzz3bfm1#

我假设是语法错误导致了这个问题。我想我已经尝试了所有可能的大小写和括号,但是你的评论帮助我把它整理好了。
修订后的守则

$().ready(function () {
        $("*").each(function (i, e) {
            if (e == '[object HTMLLIElement]') {
                console.log(i + '-' + e);
            }
        });

        var docHeight = $(document).height();
        var docWidth = $(document).width();

        var teams = $('#teamsList').children().length
        var rungs = $('#ladderList').children().length
        var blankrungs = $('#ladderList').children('.blankrung').length
        var teamrungs = $('#ladderList').children('.teamrung').length
        var teamslistHeight = docHeight - 300;
        var teamslistWidth = teamslistHeight / 25 //console.log($('#teamsList li').first.height);
        var team42 = document.getElementById('team42')

        // OK
        console.log('docHeight' + ' ' + docHeight)
        console.log('docWidth' + ' ' + docWidth)
        console.log('teams' + ' ' + teams)
        console.log('blankrungs' + ' ' + blankrungs)
        console.log('teamrungs' + ' ' + teamrungs)
        console.log('rungs' + ' ' + rungs)
        console.log('teamslistHeight' + ' ' + teamslistHeight)
        //console.log('teamslistWidth' + ' ' + teamslistWidth)
        console.log($('#teamsList').children().first())
        console.log($('#teamsList'))

        console.log($('#teamsList li').first().height());
        console.log(document.getElementById('team42'))  
        console.log(team42)                             
        console.log('#team42' + ' ' + $('#team42').text())
        console.log('innerText' + ' ' + $('#teamsList').children().first().text())
        console.log('team42' + ' ' + team42.innerText)
    });

相关问题