简体   繁体   English

JavaScript错误:未捕获的TypeError:对象[object Object]没有方法'src'

[英]Javascript error: Uncaught TypeError: Object [object Object] has no method 'src'

I'm using the following code to take images from a parse.com class and return them to the page inserted within a div. 我正在使用以下代码从parse.com类中获取图像,并将其返回到插入div中的页面。

At the moment I get a Uncaught TypeError: Object [object Object] has no method 'src' 目前,我收到了Uncaught TypeError:对象[object Object]没有方法'src'

and no images are being returned. 并且没有图像返回。

The images are stored in the class Gbadges in parse.com and as a string (URL) in the column. 图像存储在parse.com中的Gbadges类中,并作为字符串(URL)存储在该列中。

I cannot find an complete match on SO or google to this issue. 我无法在SO或Google上找到与此问题完全匹配的内容。 I presume its something to do with the image url? 我认为它与图片网址有关?

Please note that this code is is based on backbone.js framework, which lets you ebed script tags into your html 5 code. 请注意,此代码是基于骨干.js框架的,它使您可以将脚本标签嵌入到html 5代码中。

I've created a fiddle here http://jsfiddle.net/Dano007/cQgJG/ 我在这里http://jsfiddle.net/Dano007/cQgJG/创建了一个小提琴

<!doctype html>
<head>
  <meta charset="utf-8">

  <title>My Parse App</title>
  <meta name="description" content="My Parse App">
  <meta name="viewport" content="width=device-width">
  <link rel="stylesheet" href="css/reset.css">
  <link rel="stylesheet" href="css/styles.css">
  <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
  <script src="//ajax.googleapis.com/ajax/libs/jqueryui/1.10.2/jquery-ui.min.js"></script>
  <script type="text/javascript" src="http://www.parsecdn.com/js/parse-1.2.17.min.js"></script>
</head>

<body>

  <div id="main">
    <h1>You're ready to use Parse!</h1>

    <p>Read the documentation and start building your JavaScript app:</p>

    <ul>
      <li><a href="https://www.parse.com/docs/js_guide">Parse JavaScript Guide</a></li>
      <li><a href="https://www.parse.com/docs/js">Parse JavaScript API Documentation</a></li>
    </ul>

    <div style="display:none" class="error">
      Looks like there was a problem saving the test object. Make sure you've set your application ID and javascript key correctly in the call to <code>Parse.initialize</code> in this file.
    </div>

    <div style="display:none" class="success">
      <p>We've also just created your first object using the following code:</p>

        <code>
          var TestObject = Parse.Object.extend("TestObject");<br/>
          var testObject = new TestObject();<br/>
          testObject.save({foo: "bar"});
        </code>
    </div>
  </div>

  <script type="text/javascript">
    Parse.initialize("79tphN5KrDXdjJnAmehgBHgOjgE2dLGTvEPR9pEJ", "9lblofQNZlypAtveU4i4IzEpaOqtBgMcmuU1AE6Y");

    var TestObject = Parse.Object.extend("TestObject");
    var testObject = new TestObject();
      testObject.save({foo: "bar"}, {
      success: function(object) {
        $(".success").show();
      },
      error: function(model, error) {
        $(".error").show();
      }

    });

var GlobalBadges = Parse.Object.extend("GBadges");
    var query = new Parse.Query(GlobalBadges);
    query.exists("Global_Badge_Name");
    query.find({
      success: function(results) {
        // If the query is successful, store each image URL in an array of image URL's
        imageURLs = [];
        for (var i = 0; i < results.length; i++) { 
          var object = results[i];
          imageURLs.push(object.get('Global_Badge_Name'));
        }

        $('#Image01').src(imageURLs[0]); //first image
        $('#Image02').src(imageURLs[1]); //second image
        $('#Image03').src(imageURLs[2]); //third image
      },
      error: function(error) {
        // If the query is unsuccessful, report any errors
        alert("Error: " + error.code + " " + error.message);
      }
    });

 </script>

<div >
<img id="Image01"/>
<img id="Image02"/>
<img id="Image03"/>
 </div>


</body>
</html>



</body>
</html>

您需要使用attr()来设置src属性的值。

$('#Image01').attr('src',imageURLs[0]);

It is because the javascript is before the elements. 这是因为javascript在元素之前。 Place the javascript below the elements (Image elements) and it should work fine. 将javascript放在元素(图片元素)下面,它应该可以正常工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM