Javascript html()方法语法

Javascript html()方法语法,javascript,jquery,html,Javascript,Jquery,Html,下面是完整的函数/for循环。我基本上是试图从Flickr中抓取图像,并使用Lightbox呈现它们 如果您不需要所有这些信息,只需向下滚动到“这个语法有什么问题?”下面的jQuery.html()方法给我带来了麻烦。在这个实例中,语法是如何工作的?谢谢大家! $.getJSON(requestURL, function(flickrResponse) { flickrResponse.items.forEach(function (item) { // create

下面是完整的函数/for循环。我基本上是试图从Flickr中抓取图像,并使用Lightbox呈现它们

如果您不需要所有这些信息,只需向下滚动到“这个语法有什么问题?”下面的jQuery.html()方法给我带来了麻烦。在这个实例中,语法是如何工作的?谢谢大家!

$.getJSON(requestURL, function(flickrResponse) {
    flickrResponse.items.forEach(function (item) {

        // create a new a element and hide it
        var $anchor = $("<a>").hide();

        // set the href attribute of the a element to connect to the same Flickr image 
        // that the img element connects to
        $anchor.attr("href", item.media.m);

        // set the data-lightbox attribute, and use "Flickr" as the value of the attribute
        $anchor.attr("data-lightbox", "Flickr");

        // create a new JQuery element to hold the image
        // but hide it so we can fade it in
        var $img = $("<img>").hide();

        // set the attribute to the url
        // contained in the response
        $img.attr("src", item.media.m);

        // WHAT'S WRONG WITH THIS SYNTAX?
        // Use the jQuery.html() method to set the img element as the innerHTML of the a element.
        $($anchor).html($img);

        // attach the img tag to the main
        // photos element and then fade it in
        $("main.photos").append($anchor);
        $anchor.fadeIn();
    });

});
$.getJSON(请求URL,函数(flickrResponse){
flickrResponse.items.forEach(函数(项){
//创建一个新的元素并隐藏它
var$anchor=$(“”)。hide();
//设置元素的href属性以连接到同一Flickr图像
//img元素连接到的
$anchor.attr(“href”,item.media.m);
//设置数据lightbox属性,并使用“Flickr”作为该属性的值
$anchor.attr(“数据灯箱”、“Flickr”);
//创建一个新的JQuery元素来保存图像
//但是把它藏起来,这样我们就可以淡出它
变量$img=$(“
相应的HTML文件:

<!doctype html>
<html>
  <head>
    <title>Flickr App</title>
    <link href="stylesheets/styles.css" rel="stylesheet">
    <link href="stylesheets/lightbox.min.css" rel="stylesheet">
  </head>
  <body>
    <header>
    </header>

    <main>
     <div class="photos">
     </div>
    </main>

    <footer>
    </footer>
    <script src="http://code.jquery.com/jquery-latest.min.js"></script>
    <script src="javascripts/lightbox.min.js"></script>
    <script src="javascripts/app-lightBox.js"></script>
  </body>
</html>

Flickr应用程序

在JS中,您只是忘记隐藏“var$img”

如果您更改行,它应该可以工作:

var $img = $("<img>").hide();
用于:


我不知道flickrResponse在JS中是否正确,你只是忘了隐藏“var$img”

如果您更改行,它应该可以工作:

var $img = $("<img>").hide();
用于:

我不知道flickrResponse是否正确,因为每个方法接受字符串或函数

.html(htmlString)
.html(函数)

要构造图像的字符串(html)表示形式,或者使用接受
jQuery
对象的,如下所示:

$anchor.append( $img )
Per方法接受字符串或函数

.html(htmlString)
.html(函数)

要构造图像的字符串(html)表示形式,或者使用接受
jQuery
对象的,如下所示:

$anchor.append( $img )

你刚刚犯了一个简单的错误。以下是你真正需要做的:

$($anchor).html('img');

你刚刚犯了一个简单的错误。以下是你真正需要做的:

$($anchor).html('img');

看起来像是一个测试问题。
$anchor
已经是一个jQuery对象,不需要使用它周围的
$()
语法。但是如果这样做,它会失败吗?你遇到了什么问题?@Shanimal浏览器不会抛出任何错误。它只会给我一个空白页。(片刻——我正在复制它)嘿,@Zed,给我带来的麻烦不够描述性。给我们一些更多的东西!!@PeterKA在我上面粘贴的HTML文件中,正在等待JS脚本填充。我没有得到任何错误,而是一个空页面。使用浏览器调试器,它看起来像图像URL(在src属性中)正在检索,但可能未显示?这更好!请查看我的答案。看起来像一个测试问题。
$anchor
已经是jQuery对象,不必使用
$()
它的语法。但是如果你这样做,它会失败吗?你遇到了什么问题?@Shanimal浏览器没有抛出任何错误。它只给了我一个空白页。(片刻——我正在复制它)嘿,@Zed,给我带来的麻烦不够描述性。给我们一些更多的东西!!@PeterKA在我上面粘贴的HTML文件中,正在等待JS脚本填充。我没有得到任何错误,而是一个空页面。使用浏览器调试器,它看起来像图像URL(在src属性中)正在检索,但可能并没有显示?这更好!看看我的答案。这是否将img元素设置为a元素的innerHTML?是的,确实如此。我起初认为这一点,但我尝试过,事实上,将jQuery元素
$img
传递给
html
方法具有预期的结果。(设置其html).可能Bruno Costa的答案是正确的…这是否将img元素设置为元素的innerHTML?是的,确实如此。我起初认为这一点,但我尝试过,事实上,将jQuery元素
$img
传递给
html
方法具有预期的结果。(设置其html)也许布鲁诺·科斯塔的回答是对的…谢谢!为什么在“main.photos”中需要一个空格?谢谢!为什么在“main.photos”中需要一个空格?
$($anchor).html('img');