List、Count 元素,但属性不可用

List, Count elements, but properties not available

提问人:andybrum 提问时间:7/26/2021 更新时间:7/26/2021 访问量:26

问:

我能够列出所有元素并获取 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
jQuery 属性 语法错误

评论

1赞 T.J. Crowder 7/26/2021
这是一系列错别字级别的错误。(我们都犯过错别字级别的错误。我会给你写一个列表,但我建议通读jQuery API文档,以及你正在使用的东西(例如,getElementById)的一般文档。例如,应该是 -- 接受 ID 值,而不是 CSS 选择器。document.getElementById('#team42')document.getElementById('team42')getElementById
1赞 T.J. Crowder 7/26/2021
列表如下:“console.log($('#teamsList li').first.height); 并且都是方法,而不是数据属性,您需要使用它们来调用它们:“console.log(document.getElementById('#team42'))” 正如我上面提到的,ID 值中不应该有。console.log('#team42' + ' ' + $('#team42')。Text) 它不是 ,而且它又是一个方法,而不是一个数据属性。可能还有其他类似的错误,请密切关注细节并根据需要参考文档。祝您编码愉快!firstheight()console.log($('#teamsList li').first().height());#textText

答:

0赞 andybrum 7/26/2021 #1

我以为是语法错误导致了问题。我以为我已经尝试了所有可能的案例和括号,但您的评论帮助我对其进行了分类。

修订后的代码是

    $().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)
    });