javascript - 在程序启动时禁用轮播按钮

标签 javascript jquery html css

我有一个带 3 个 Pane 的旋转木马。首次加载页面时,我希望禁用左按钮,以便用户可以在 Pane 中向后循环。

我有一个有效的 diableButton 函数:

function disableLButton()
{
    $(".carouselBtnLeft").attr("disabled",true);
}

当我返回到第一个 Pane 时调用它时,这工作正常。

我不希望它在程序首次运行时也被禁用,我尝试使用 firstRun bool 简单地调用该函数,但它不起作用:

var firstRun = true;

if(firstRun == true)
{
    disableLButton();
    firstRun=false; 
}

我确信有一个简单得令人作呕的解决方案...

已解决:

我在正文中添加了一个加载函数:

<body onload="load()">

然后在加载函数中:

function load()
{
    disableLButton(true);
}

最佳答案

您需要使用 prop() 而不是 attr()

来设置 disabled 属性
function disableLButton()
{
     $(".carouselBtnLeft").prop("disabled",true);
}

请参阅此处了解 API 方法 prop()

http://api.jquery.com/prop/

The difference between attributes and properties can be important in specific situations. Before jQuery 1.6, the .attr() method sometimes took property values into account when retrieving some attributes, which could cause inconsistent behavior. As of jQuery 1.6, the .prop() method provides a way to explicitly retrieve property values, while .attr() retrieves attributes.

disabled 属性被认为是一个 bool 属性:

http://www.w3.org/TR/html4/intro/sgmltut.html#h-3.3.4.2

并且在删除禁用属性时不要忘记使用这个:

 $(".carouselBtnLeft").removeProp("disabled");

您不要将其设置为 false,因为即使存在 disabled 属性,即使它没有值也会被视为 true.. 所以使用 removeProp() 删除 disabled 属性

关于javascript - 在程序启动时禁用轮播按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19566088/

相关文章:

firefox-addon - 在 Firefox 扩展中导入 Javascript 模块

jquery - 刷新页面或执行大量 JQuery?

javascript - 使用 Jquery 调用下一个 td

javascript - HTML/CSS 在 css 掩码中绘制方形窗口

javascript - 在纯 Javascript 中复制元素和更改类

javascript - 粘性滚动容器在 IE 中不起作用

javascript - 如何更改 float div 的宽度以填充它们占据的所有空间?

javascript - requestAnimationFrame 没有触发提供给它的函数

javascript - 在打印屏幕 AngularJS 顶部获取空白

javascript - Bootstrap 中的中间中心 div