Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/78.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
尝试向按钮添加javascript函数_Javascript_Jquery_Function_Button - Fatal编程技术网

尝试向按钮添加javascript函数

尝试向按钮添加javascript函数,javascript,jquery,function,button,Javascript,Jquery,Function,Button,我对HTML比较陌生,所以请容忍我。我正在尝试添加一个简单的javascript函数,该函数已经提供给我,可以将图像从Flickr抓取到一个按钮上。当我点击按钮时,什么也没发生。我不确定错误发生在哪里,但我认为这与函数执行不正确有关 HTML: <!DOCTYPE html> <html> <head> <title>getJSON</title> <meta charset=

我对HTML比较陌生,所以请容忍我。我正在尝试添加一个简单的javascript函数,该函数已经提供给我,可以将图像从Flickr抓取到一个按钮上。当我点击按钮时,什么也没发生。我不确定错误发生在哪里,但我认为这与函数执行不正确有关

HTML:

   <!DOCTYPE html>
    <html>
    <head>
        <title>getJSON</title>
        <meta charset="utf-8" />
        <script src="jquery-3.1.1.min.js"></script>
    </head>
    <body>
    <button onclick="myFunction()">Click me</button>
    <script>
    function myFunction() 
    {  //Functions don't need names. This function will simply run when the page loads
      var flickerAPI = "http://api.flickr.com/services/feeds/photos_public.gne?jsoncallback=?";
      $.getJSON( flickerAPI, 
      {  // The "$" simply refers to jQuery. This calls the getJSON function that is found inside jquery-3.1.1.min.js
        tags: "Baruch College", //Some filter information in the format set by Flickr, who owns the JSON
        tagmode: "any",
        format: "json"
      })
        .done(function( data ) 
        { //Here, a an unnamed function is created made into the event handler for the "done" event... in other words, this is the code that will manifest itself when the getJSON is done.
          $.each( data.items, function( i, item ) 
          {
            $( "<img>" ).attr( "src", item.media.m ).appendTo( "#images" );
            if ( i === 5 ) 
            {
              return false;
            }
          });
        });
    }
    </script>

    </body>
</html>

getJSON
点击我
函数myFunction()
{//函数不需要名称。此函数只需在页面加载时运行即可
var flickerAPI=”http://api.flickr.com/services/feeds/photos_public.gne?jsoncallback=?";
$.getJSON(flickeAPI,
{//“$”只是指jQuery。它调用jQuery-3.1.1.min.js中的getJSON函数
标记:“Baruch College”,//一些过滤信息的格式由Flickr设置,Flickr拥有JSON
tagmode:“任何”,
格式:“json”
})
.完成(功能(数据)
{//在这里,一个未命名的函数被创建到“done”事件的事件处理程序中……换句话说,这是当getJSON完成时将显示自己的代码。
$.each(data.items,function(i,item)
{
$( "

我上传了HTML中提到的jquery脚本:

您需要指定检索图像后图像的放置位置。在JavaScript代码中,您可以看到,它为每个图像创建一个
标记,并将其附加到元素
id=“images”

只需将
id=“images”
添加到按钮元素,如下所示:

单击我


单击按钮时,图像将放置在按钮内。

您必须在本地服务器上运行页面。当前运行页面的服务器是什么?