javascript - ul li $(this).text() 什么也没显示

标签 javascript jquery twitter-bootstrap

解决了!实际上问题出在 $(document).ready(function(){//});我使用后,效果很好。感谢您的帮助,并对如此愚蠢的错误表示歉意。

我正在尝试从 Bootstrap 下拉菜单中选择 li。它有效,但是当我想获取所选 li 的文本时,它什么也不显示。

<div class="btn-group">
    <a class="btn dropdown-toggle" data-toggle="dropdown" href="#">
        <font id="dropdownMenu" >inch</font>
        <span class="caret"></span>
    </a>
    <ul id="dropdownList" class="dropdown-menu">
        <li><a href="#">inch</a></li>
        <li><a href="#">cm</a></li>
        <li><a href="#">mm</a></li>
    </ul>
</div>

js:

$('#dropdownList li').click(function () {
    console.log($(this).text());
    document.getElementById('dropdownMenu').innerHTML = $(this).text();
});

我可以知道为什么它不起作用吗?

谢谢!

最佳答案

使用Jquery:

JSFiddle

$('#dropdownList li').click(function () {
    console.log($(this).text());
    $('#dropdownMenu').text($(this).text());
});

关于javascript - ul li $(this).text() 什么也没显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35841538/

相关文章:

javascript - 如何使用 Nexmo 建立多个 WebSocket 连接

javascript - 两个嵌套 ng-repeat 上的 Angular 自定义过滤

html - 无法使用 bootstrap.css 更改导航栏的高度

javascript - Handlebars 和文本区域

javascript - 如何更改 angularJS+Bootstrap 中的行?

javascript - 如何在鼠标悬停时暂停 Bootstrap 轮播并在鼠标移开时恢复它?

javascript - 使用 Prototype 的 $() 解析responseXML

javascript - W3schools 上 AJAX 示例的问题

javascript - 在回调函数中保留 "this"

javascript - 有没有办法在媒体查询中禁用 css 类?