admin管理员组

文章数量:1168609

Here's the documentation for this plugin (There's only two functions.)

$(document).ready(function() {
    function load(num) {
        $('#content').load(num +".html");
    }

    $.history.init(function(url) {
        load(url == "" ? "1" : url);
    });

    $('#ajax-links a').live('click', function(e) {
        var url = $(this).attr('href');
        url = url.replace(/^.*#/, '');
        $.history.load(url);
        return false;
    });
});

Here's the html:

<body>
  <h1>jQuery History Plugin Ajax Sample</h1>
  <div id="ajax-links">
    <ul>
      <li><a href="#1">load 1.html</a></li>
      <li><a href="#2">load 2.html</a></li>
      <li><a href="#3">load 3.html</a></li>
    </ul>
    <div id="content"></div>
    <hr />
  </div>
  <p>[<a href="../">All samples</a>] [<a href="">Project home</a>]</p>
</body>

Here's the documentation for this plugin (There's only two functions.) http://tkyk.github.com/jquery-history-plugin/#documentation

$(document).ready(function() {
    function load(num) {
        $('#content').load(num +".html");
    }

    $.history.init(function(url) {
        load(url == "" ? "1" : url);
    });

    $('#ajax-links a').live('click', function(e) {
        var url = $(this).attr('href');
        url = url.replace(/^.*#/, '');
        $.history.load(url);
        return false;
    });
});

Here's the html:

<body>
  <h1>jQuery History Plugin Ajax Sample</h1>
  <div id="ajax-links">
    <ul>
      <li><a href="#1">load 1.html</a></li>
      <li><a href="#2">load 2.html</a></li>
      <li><a href="#3">load 3.html</a></li>
    </ul>
    <div id="content"></div>
    <hr />
  </div>
  <p>[<a href="../">All samples</a>] [<a href="http://github.com/tkyk/jquery-history-plugin">Project home</a>]</p>
</body>
Share Improve this question asked Aug 11, 2011 at 9:01 user784637user784637 16k33 gold badges96 silver badges159 bronze badges 4
  • 1 Possible duplicates: stackoverflow.com/questions/1771786/question-mark-in-javascript stackoverflow.com/questions/1688337/javascript-if-alternative stackoverflow.com/questions/3322704/javascript-notation stackoverflow.com/questions/4278232/… stackoverflow.com/questions/6813840/… – JJJ Commented Aug 11, 2011 at 9:03
  • This is basic ternary operator of javascript refer msdn.microsoft.com/en-us/library/be21c7hw(v=vs.94).aspx – Devjosh Commented Aug 11, 2011 at 9:04
  • Possible duplicate of Javascript Ternary operator – Jürgen Thelen Commented Aug 11, 2011 at 9:05
  • Possible duplicate of Question mark and colon in JavaScript – Boghyon Hoffmann Commented Jul 1, 2019 at 8:20
Add a comment  | 

3 Answers 3

Reset to default 30
load(url == "" ? "1" : url);

The question mark here is a a ternary if operation, Simply put, it is a short, inline if statement.

Expanded out, the statement would look something like this:

if (url == "")
    load("1");
else
    load(url);

If the statement before the question mark evaluates to true, then the left-hand side of the colon is used, otherwise (if it is false) the right-hand side is used. You can also nest this, though it isn't always a good idea (for readability).

Its shorthand for:

If (url == ""){
   load("1");
}
else {
   load(url);
}

Ie. If url equals "" then return "1", otherwise, return url

In your example, if the url equals "" then, 1.html will be loaded, otherwise, url + ".html" will be loaded

It is a ternary operation.

本文标签: javascriptWhat does the question mark mean in this functionStack Overflow