【发布时间】:2014-01-15 22:46:25
【问题描述】:
我有一个显示一些数据的网页。如果有更多的数据不能一次显示,我有一个导航栏,可以导航到下一页。共有 4 个按钮; “第一页”、“上一页”、“下一页”和“最后一页”。如果一个在第 1 页,则需要禁用前 2 个按钮,如果在最后一页,则需要禁用最后 2 个按钮。
现在我已经看到了一堆关于如何做到这一点的示例(也在 stackOverflow 上),问题是:它们都不起作用!
我试过(JS):
doc.getElmByID("button")
button.setAttribute('disabled', true/'disabled')
// (meaning both true and disabled have been tried with no luck).
jQuery:
$(button).attr('disabled', true/'disabled')
$(button).prop('disabled', true/'disabled')
$(button).button('disabled')
现在其中几个确实做了一些事情,它们删除了“cursor=pointer”选项,但点击事件仍然存在。
--编辑--
HTML 代码
html += '<div id="naviLeft"><img type="button" id="First" class="grey" src="images/dobbelt-pil-disabled.png" title="First page"> </img>';
html += '<img type="button" id="Previous" class="grey" src="images/pil-disabled.png" title="Previous page"></img></div>';
html += '<div id="changeRecords"></div>';
html += '<div id="naviRight"><img type="button" id="Next" class="grey" src="images/pil-r-disabled.png" title="Next page"> </img>';
html += '<img type="button" id="Last" class="grey" src="images/dobbelt-pil-r-disabled.png" title="Last page"></img> </div>';
document.getElementById("navi").innerHTML = html;
$('#First').click(function(){currentTable.First(), setNavigation()});
$('#Previous').click(function(){currentTable.Previous(), setNavigation()});
$('#Next').click(function(){currentTable.Next(), setNavigation()});
$('#Last').click(function(){currentTable.Last(), setNavigation()});
...
setNavigation = function()
{
if (currentTable)
{
// currentTable.total and currentTable.currentPage are placeholders for current Page and total pages.
var current = currentTable.currentPage;
var total = currentTable.total;
var first = $('#First');
var previous = $('#Previous');
var next = $('#Next');
var last = $('#Last');
if (current == 1)
{
$(first).attr('src', 'images/dobbelt-pil-disabled.png');
$(first).css('cursor', 'default');
$(previous).attr('src', 'images/pil-disabled.png');
$(previous).css('cursor', 'default');
}
if (current > 1)
{
$(first).attr('src', 'images/dobbelt-pil.png');
$(first).css('cursor', 'pointer');
$(first).hover(function(){$(this).attr('src', 'images/dobbeltpil-chosen.png');},function(){$(this).attr('src', 'images/dobbelt-pil.png');});
$(previous).attr('src', 'images/pil.png');
$(previous).css('cursor', 'pointer');
$(previous).hover(function(){$(this).attr('src', 'images/pil-chosen.png');},function(){$(this).attr('src', 'images/pil.png');});
}
【问题讨论】:
-
:有什么特别的错误吗??
-
foo.disabled = true;whitfoo是按钮。 (即foo = document.getElementById('yourButtonId')。Here's a fiddle -
如果按钮元素然后
$("button").prop('disabled', true);如果id然后$("#button").prop('disabled', true); -
对不起。忘记。 foo.disabled = true 也不起作用
-
button.setAttribute('disabled', true/'disabled') // (表示 true 和 disabled 都试过了,但都没有运气)。
标签: javascript jquery