0

这是我的示例代码。如果您单击下一个按钮,它将显示该页面的下一个内容。如果您再单击一次,则会显示空白页面,因为内容不存在。如何在 div 中查找内容是否可用?

<!DOCTYPE html>
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
<style type="text/css" media="screen">
body {background-color:white; font:16px Helvetica, Arial; color:black;}
.pagination {margin:auto; display:block; height:275px; width:65%; position:relative; overflow:hidden;     border:1px solid black;}
</style>
<script>
$(document).ready(function() {
//$('.pagination').children().wrapAll('<div class="content"/>');
$('.next').click(function() {
    var height = 10;
    var current = ($('.content').css('margin-top') || '0px');
    var pag = document.getElementById("page");
    //for(i=0; i<pag.
    $('.content').css('margin-top',current.substring(0,current.length-2) - $('.pagination').height() + 'px');
});
});

function clk()
{
    var idc = document.getElementById("c");
    //console.log(idc);
}

function test()
{
    var idc = document.getElementById("c");
    var tn = idc.childNodes[0];
    var sp = document.createElement("span");
    var range = document.createRange();

    range.selectNode(tn);
    range.surroundContents(sp);

    console.log("test" + sp.offsetTop);

    return false;
}
</script>
</head>
<body>
<div id="page" class="pagination" style="padding:5px">
<div class="content">
    <p>The House of Representatives shall be composed of Members chosen every second Year by the People of the several States, and the Electors in each State shall have the Qualifications requisite for Electors of the most numerous Branch of the State Legislature.</p>
    <p>No Person shall be a Representative who shall not have attained to the Age of twenty five Years, and been seven Years a Citizen of the United States, and who shall not, when elected, be an Inhabitant of that State in which he shall be chosen.</p>
    <p>(Representatives and direct Taxes shall be apportioned among the several States which may be included within this Union, according to their respective Numbers, which shall be determined by adding to the whole Number of free Persons, including those bound to Service for a Term of Years, and excluding Indians not taxed, three fifths of all other Persons.) (The previous sentence in parentheses was modified by the 14th Amendment, section 2.) The actual Enumeration shall be made within three Years after the first Meeting of the <span id="c" onclick="clk()">Congress</span> of the United States, and within every subsequent Term of ten Years, in such Manner as they shall by Law direct. The Number of Representatives shall not exceed one for every thirty Thousand, but each State shall have at Least one Representative; and until such enumeration shall be made, the State of New Hampshire shall be entitled to chuse three, Massachusetts eight, Rhode Island and Providence Plantations one, Connecticut five, New York six, New Jersey four, Pennsylvania eight, Delaware one, Maryland six, Virginia ten, North Carolina five, South Carolina five and Georgia three.</p>
    <p>When vacancies happen in the Representation from any State, the Executive Authority thereof shall issue Writs of Election to fill such Vacancies.</p>
    <p>The House of Representatives shall chuse their Speaker and other Officers; and shall have the sole Power of Impeachment.</p>
    </div>
</div>
<ul>
<li>Previous</li>
<li class="next">Next</li>
<li><a href="" onclick="return test()">Click</a></li>
</body>
</html>
4

4 回答 4

2
if ($('div').html()) {
    // Not empty
} else {
    // Empty
}

http://jsfiddle.net/bmzmN/

于 2013-04-02T09:07:40.647 回答
0

没有看到您的代码,但一般检查$('#yourdiv').html()是否为空

于 2013-04-02T09:02:44.757 回答
0

只是对 Kaloyan 所说的话的补充。

if ($.trim($('div').html())) {
    $('p').text('Not empty');
} else {
    $('p').text('Empty');
}

您可以使用上述内容,以便甚至可以修剪空格。

于 2013-04-02T09:13:40.343 回答
0

如果 div 的长度为 0,则表示该 div 内没有内容。你也可以这样检查。请看一下

http://jsfiddle.net/X5r8r/1108/

$("#div_1").html().trim().length
$("#div_2").html().trim().length
于 2013-04-02T09:20:00.120 回答