我花了很长时间搞清楚我不应该使用clear()作为Javascript中函数的名称:
<head>
<script type="text/javascript" src="Array.js"></script>
</head>
<body>
Hello!!!!<br>
<button type="button" onClick="clear()" id="ppp">Shoo!</button><br>
<button type="button" onClick="add()" id="add">Add a few elements</button><br>
<button type="button" onClick="check()" id="check">Check the array</button><br>
<p id="results">Results will appear here.</p>
<script type="text/javascript">
initialize();
</script>
</body>
Run Code Online (Sandbox Code Playgroud)
这是Array.js:
var results;
function initialize(){
results = document.getElementById("results");
}
function add() {
results.firstChild.data="add";
}
function clear() {
results.firstChild.data = "Hello?";
}
function check() {
results.firstChild.data = "check";
}
Run Code Online (Sandbox Code Playgroud)
症状:单击"添加"和"检查"按钮可以得到我期望的结果,但单击"清除"按钮不会执行任何操作.
如果我将clear()重命名为clearxyz(),它可以正常工作.
我的问题:
非常感谢.编辑:我正在使用Firefox 6.0,我添加了换行符以显示Array.js的启动位置.
在我的HTML中,我lang在script标签中定义了函数并添加了"Test Fire!" 点击时必须调用lang的按钮:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Testing Functions</title>
<script type="text/javascript">
function lang() {
alert("Hello, World! It's JavaScript this time");
}
</script>
</head>
<body>
<form action="">
<input type="button" value="Test Fire!" onclick="lang();">
</form>
</body>
</html>Run Code Online (Sandbox Code Playgroud)
但是,如果我单击按钮,我会收到此错误:
未捕获的TypeError:
lang不是函数
但是,如果我将函数名称更改lang为其他任何内容,则此代码可以正常工作.
我知道这一定是非常基本的东西,但我不了解范围是如何工作的。我希望closed整个 JavaScript 文件都知道该变量。
我有类似的东西(在 jQuery 中):
\n\nvar closed = 0;\n\n$(function(){\n console.log(closed);\n});\nRun Code Online (Sandbox Code Playgroud)\n\n但closed被记录为false. load我用和函数尝试了很多东西onload,但失败了。
这是一个非常简单的代码,我想尝试并运行,因为我想学习一些JavaScript基础知识.该代码适用于Internet Explorer和Firefox,但不适用于chrome.我觉得我必须错过一些非常愚蠢的东西.
var frame = 2;
function animate(){
if(frame == 1){
frame = frame + 1;
document.getElementById("animate").src = "walking1.png";
}
else if (frame == 2){
frame = frame + 1;
document.getElementById("animate").src = "walking2.png";
}
else{
frame = 1;
document.getElementById("animate").src = "walking3.png";
}
}Run Code Online (Sandbox Code Playgroud)
<p> clicking the button will change the image.</p>
<img id="animate" src="walking1.png">
<button onclick="animate()">click me to animate</button>Run Code Online (Sandbox Code Playgroud)
使用的图片保存在同一个文件夹中.
我有一个相当简单的页面,其中包含侧边栏导航和用于加载内容的iFrame.
我想通过单击侧栏导航中的链接来更改iFrame的内容.我正在使用javascript来更改document.element的源(.src).
我已经阅读了很多文章(StackOverflow)并且代码应该可以工作,但事实并非如此.
下面的代码是我创建的html页面,它包含标签中的JS.
<head>
<meta charset="UTF-8">
<title>Untitled Document</title>
<link href="css/default.css" rel="stylesheet" type="text/css">
<script type="text/javascript">
function getContent{
document.getElementById("contentFrame").src="LoremIpsum.html";
}
</script>
</head>
<body>
<div class="sidebar"><h1>Technical Documentation</h1>
<ul>
<li>Configuration Guides</li>
<li>API Guides</li>
<li><a href="" onclick='getContent()'> LoremIpsum</a></li>
</ul>
<!-- <button onclick="getContent()">Lorem Ipsum</button> -->
</div>
<iframe class="content" id="contentFrame" src="dummy.html">
</iframe>
</body>
Run Code Online (Sandbox Code Playgroud) 我想将click事件绑定到每个事件,<p>但它似乎无法正常工作.
当我运行脚本时,我立即得到三个警报.我只想在点击三个中<p>'的任何一个时获得它们.
谁能告诉我我做错了什么?
编辑:Sry这就是它的样子.HTML正如它应该的那样:<p class="special">text text text</p>等等
(function () {
var myFunction = function (theP) {
alert(theP.id)
}
window.onload = function () {
var pTags = document.getElementsByTagName('p'),
pLength = pTags.length,
i;
for (i = 0; i < pLength; i += 1) {
if(pTags[i].className == 'special'){
pTags[i].onClick = myFunction(pTags[i]);
}
};
}
})();
Run Code Online (Sandbox Code Playgroud)
PS.我不能使用jQuery atm
> <script type="text/javascript"
src="http://maps.google.com/maps/api/js?sensor=true">
</script>
<script type="text/javascript">
function initialize() {
var latlng = new google.maps.LatLng(-34.397, 150.644);
var myOptions = {
zoom: 8,
center: latlng,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
var map = new google.maps.Map(document.getElementById("map_canvas"),
myOptions);
marker = new google.maps.Marker({
position : latlng,
title : "hello world",
draggable : true
});
marker.setMap(map)
}
function write(){
//var positon = marker.getPosition()
alert('position')
}
</script>
</head>
<body onload="initialize()">
<div id="map_canvas"></div>
<input type='button' value ='position' onClick="write()">
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
每当我点击名为位置的按钮时,地图就会消失,为什么会这样呢?
这是谷歌Chrome版本53.0.2785.101(64位)的问题.我尝试运行一个简单的html文件,当我使用'slot'这个词时,它会抛出错误'slot.testFun不是函数'.
<html>
<head>
<title>TEST</title>
</head>
<body>
<a href="#" onclick="slot.testFun();">Click Here</a>
<script type="text/javascript">
var slot = {
testFun: function(){
console.log('clicked');
}
}
</script>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
我们的代码中没有与此变量冲突.它只是浏览器,不允许在这个最新版本中使用变量名.
如果您使用除"slot"之外的任何其他名称,它可以正常工作.
javascript ×8
html ×2
ecmascript-6 ×1
google-maps ×1
iframe ×1
onclick ×1
scope ×1
typeerror ×1
variables ×1