admin管理员组

文章数量:1387285

I'm new to javascript and I have searched for this issue across the web. It might be a simple fix, but I just can't get rid of this error (only in chrome, not FF).

This is the javascript I have:

<script>
    function clikked(){
        window.alert("sometext");
    }
</script>

In my html I have many DIVs like this:

....
<div class="tiles2" id="1180" name="1180" onclick="clikked()"></div>
<div class="tiles2" id="1181" name="1180" onclick="clikked()"></div>
....

What am I doing wrong here? I always get that error in chrome... remember, I'm new to javascript, so every info will be helpful!

edit: Here's the php part

for ($i = 0; $i<2000; $i++) {
    echo '<div class="tiles2" id="'.$i.'" name="'.$i.'" onClick="clikked()" ></div>';
}   

edit 2: Here's the full Test.php code:

<!DOCTYPE html PUBLIC "-//IETF//DTD HTML 2.0//EN">
<HTML>
   <HEAD>
      <TITLE>
         Test
      </TITLE>
   </HEAD>
<BODY>
<style type="text/css">
.tiles2 {
    width: 16px;
    height: 12px;
    float: left;
    background-color: white;
    opacity: 0.9;
}

.tiles2:hover {
    opacity: 0.2;
}
}
</style>


<?php
echo '<div style="width: 800px; height: 480px; background-image: url(australia/austr.png); margin: 0px auto">';

for ($i = 0; $i<20; $i++) {
    echo '<div class="tiles2" id="'.$i.'" name="'.$i.'" onClick="clikked()" ></div>';
}
echo '</div>';
?>

<script>
    function clikked(){
        alert("sometext");
    }
</script>
</BODY>
</HTML>

edit 3: ok, i've "solved" it. I was running this on localhost with xampp. it works when i upload it to the server. does anyone know why?

I'm new to javascript and I have searched for this issue across the web. It might be a simple fix, but I just can't get rid of this error (only in chrome, not FF).

This is the javascript I have:

<script>
    function clikked(){
        window.alert("sometext");
    }
</script>

In my html I have many DIVs like this:

....
<div class="tiles2" id="1180" name="1180" onclick="clikked()"></div>
<div class="tiles2" id="1181" name="1180" onclick="clikked()"></div>
....

What am I doing wrong here? I always get that error in chrome... remember, I'm new to javascript, so every info will be helpful!

edit: Here's the php part

for ($i = 0; $i<2000; $i++) {
    echo '<div class="tiles2" id="'.$i.'" name="'.$i.'" onClick="clikked()" ></div>';
}   

edit 2: Here's the full Test.php code:

<!DOCTYPE html PUBLIC "-//IETF//DTD HTML 2.0//EN">
<HTML>
   <HEAD>
      <TITLE>
         Test
      </TITLE>
   </HEAD>
<BODY>
<style type="text/css">
.tiles2 {
    width: 16px;
    height: 12px;
    float: left;
    background-color: white;
    opacity: 0.9;
}

.tiles2:hover {
    opacity: 0.2;
}
}
</style>


<?php
echo '<div style="width: 800px; height: 480px; background-image: url(australia/austr.png); margin: 0px auto">';

for ($i = 0; $i<20; $i++) {
    echo '<div class="tiles2" id="'.$i.'" name="'.$i.'" onClick="clikked()" ></div>';
}
echo '</div>';
?>

<script>
    function clikked(){
        alert("sometext");
    }
</script>
</BODY>
</HTML>

edit 3: ok, i've "solved" it. I was running this on localhost with xampp. it works when i upload it to the server. does anyone know why?

Share Improve this question edited Apr 5, 2018 at 12:30 TheGeekZn 3,92411 gold badges57 silver badges92 bronze badges asked May 3, 2014 at 22:21 user3600163user3600163 211 gold badge1 silver badge3 bronze badges 5
  • I don't see anything wrong with your syntax and it works as expected in my Chrome. When does this error appear? What's the structure of your html? Where's this javascript located and how do you plug it in? Try to strip the html file down to localize the problem, i.e. remove "many DIVs" and leave just two of them. Will you still have the problem then? – Stanislav Kniazev Commented May 3, 2014 at 22:47
  • I just added the php part. I'm generating 2000 divs with an onclick event. It doesn't work with only one. I'm actually trying to insert the code into the wordpress page.php file. I'm now trying to test it in a standalone file only with the code that is not working. Will update soon. – user3600163 Commented May 3, 2014 at 22:50
  • was it bad code in the cache? was there an element named clikked or alert? – epascarello Commented Jul 23, 2014 at 0:42
  • I ran your code and it works in chrome jsfiddle/f157vc06 – Huangism Commented Sep 24, 2014 at 18:29
  • Why does this keeps getting bumped by Community ♦? – user7393973 Commented Mar 2, 2017 at 11:41
Add a ment  | 

5 Answers 5

Reset to default 1

I had the same problem. I solved it by simply renaming the function being called and the name you call.

Thus, my savData() was renamed to savKeyData() and my function savData() {} was renamed to savKeyData() {} and it worked.

Place your script tag at the bottom of body.

Alternatively, use the onLoad or onDocumentReady event.

<div class="tiles2" id="1180" name="1180" onclick="clikked()"></div>
<div class="tiles2" id="1181" name="1180" onclick="clikked()"></div>
<script type="text/javascript">
    function clikked(){
        window.alert("sometext");
    }
</script>

JSBin: http://jsbin./ronacuka/1/

Try this. Remove from your markup onclick and put in your js at bottom page this:

var i, tiles = document.getElementsByClassName('tiles2');
for(i = tiles.length; i--;) {
    tiles[i].addEventListener("click", clikked, false);
}

function clikked(){
    alert("some text");
}

Unless you're using HTML5, your id and name attribute must start with a letter.

http://www.w3/TR/html4/types.html#type-id

ID and NAME tokens must begin with a letter ([A-Za-z]) and may be followed by any number of letters, digits ([0-9]), hyphens ("-"), underscores ("_"), colons (":"), and periods (".").

Change your doctype to <!doctype html> and see if that works.

Make the function like so:

<script type="text/javascript">
    function clikked(){
        alert("sometext");
    }
</script>

I think the main problem is that you're using the window global scope.

本文标签: javascriptUncaught TypeError object is not a functionStack Overflow