JavaScript 变量未定义
我正在为移动浏览器使用 jQTouch 编写 HTML5 应用程序。
我使用这样的代码设置变量:
var activeproduct; // globally in js file
然后我有一个 ul li 来设置变量,如下所示:
<li class="arrow" onclick="activeproduct=1;">
<a href="#productClickThru">Product</a>
</li>
这在 FF 上运行良好,但当我在 iPhone 的 Safari 上尝试时,我收到了未定义的错误当我尝试在函数内使用 activeproduct 时。
我是否没有正确设置 var
?非常感谢任何帮助。
Billy
根据这里的要求,提供了更多代码(请注意,所有列表项最终都将动态生成):
我的 javascript 文件:
var jQT = new $.jQTouch({
statusBar: 'black'
});
var activeproduct;
var activeroom;
var activearea;
var last_hash;
$(function(){
$(window).bind( 'hashchange',
function(){
console.log('unloading ' + last_hash);
$(window).trigger('unload' + last_hash);
last_hash = location.hash;
console.log('loading ' + location.hash);
$(window).trigger('load' + location.hash);
});
$(window).bind('unload#productsMenu', function() {
$('#productsMenuContent > *').remove();
});
$(window).bind('load#productsMenu',
function() {
console.log('Products menu loaded');
$('<li class="arrow" onclick="activeproduct=1;"><a href="#productClickThru">Strip</a></li>').appendTo($('#productsMenuContent'));
$('<li class="arrow "onclick="activeproduct=2;"><a href="#productClickThru">Prep</a></li>').appendTo($('#productsMenuContent'));
$('<li class="arrow" onclick="activeproduct=3;"><a href="#productClickThru">Heavy Prep</a></li>').appendTo($('#productsMenuContent'));
$('<li class="arrow" onclick="activeproduct=4;"><a href="#productClickThru">Line</a></li>').appendTo($('#productsMenuContent'));
$('<li class="arrow" onclick="activeproduct=5;"><a href="#productClickThru">Finished Paper</a></li>').appendTo($('#productsMenuContent'));
$('<li class="arrow" onclick="activeproduct=6;"><a href="#productClickThru">Emulsion</a></li>').appendTo($('#productsMenuContent'));
$('<li class="arrow" onclick="activeproduct=7;"><a href="#productClickThru">Satin</a></li>').appendTo($('#productsMenuContent'));
});
$(window).bind('load#productClickThru',
function() {
alert(activeproduct);
console.log('Room: '+activeroom);
console.log('Area: '+activearea);
console.log('Product: '+activeproduct);
if( activeproduct == 1 ) {
$('#productClickThru > .toolbar > :header').html('Strip');
$('#productClickThru').find('.room-label').html('Room: '+activeroom);
$('#productClickThru').find('.area-label').html('Area: '+activearea);
} else if( activeproduct == 2 ) {
$('#productClickThru > .toolbar > :header').html('Prep');
$('#productClickThru').find('.room-label').html('Room: '+activeroom);
$('#productClickThru').find('.area-label').html('Area: '+activearea);
} else if( activeproduct == 3 ) {
$('#productClickThru > .toolbar > :header').html('Heavy Prep');
$('#productClickThru').find('.room-label').html('Room: '+activeroom);
$('#productClickThru').find('.area-label').html('Area: '+activearea);
} else if( activeproduct == 4 ) {
$('#productClickThru > .toolbar > :header').html('Line');
$('#productClickThru').find('.room-label').html('Room: '+activeroom);
$('#productClickThru').find('.area-label').html('Area: '+activearea);
} else if( activeproduct == 5 ) {
$('#productClickThru > .toolbar > :header').html('Finished Paper');
$('#productClickThru').find('.room-label').html('Room: '+activeroom);
$('#productClickThru').find('.area-label').html('Area: '+activearea);
} else if( activeproduct == 6 ) {
$('#productClickThru > .toolbar > :header').html('Emulsion');
$('#productClickThru').find('.room-label').html('Room: '+activeroom);
$('#productClickThru').find('.area-label').html('Area: '+activearea);
} else if( activeproduct == 7 ) {
$('#productClickThru > .toolbar > :header').html('Satin');
$('#productClickThru').find('.room-label').html('Room: '+activeroom);
$('#productClickThru').find('.area-label').html('Area: '+activearea);
}
});
});
index.php 文件:
<div id="productClickThru" class="page">
<div class="toolbar"><h1 name="title"></h1>
<a class="back button" href="#">Back</a>
</div>
<ul>
<li><span class="room-label"></span></li> //gets set by JS
<li><span class="area-label"></span></li> //gets set by JS
<li>
<select id="quantity">
<optgroup label="quantity">
<option value ="10">10</option>
<option value ="20">20</option>
<option value ="20">30</option>
<option value ="20">40</option>
<option value ="20">50</option>
<option value ="20">60</option>
<option value ="20">70</option>
<option value ="20">80</option>
<option value ="20">90</option>
<option value ="20">100</option>
<option value ="20">150</option>
</optgroup>
</select>
</li>
<li><textarea placeholder="Notes" ></textarea></li>
</ul>
<a href="#" class="submit whiteButton">Save</a>
<!-- #new-quote close -->
</div>
I'm writing an HTML5 app with jQTouch for mobile browsers.
I'm setting variables using code like this:
var activeproduct; // globally in js file
Then I have a ul li
that sets the varialbe like so:
<li class="arrow" onclick="activeproduct=1;">
<a href="#productClickThru">Product</a>
</li>
This works fine on FF but when I try it on iPhone's Safari I'm getting an undefined error when I try to use activeproduct inside a function.
Am I not setting up the var
properly? Any help most appreciated.
Billy
As requested here is some more code (please note all list items will be generated dynamically eventually):
My javascript file:
var jQT = new $.jQTouch({
statusBar: 'black'
});
var activeproduct;
var activeroom;
var activearea;
var last_hash;
$(function(){
$(window).bind( 'hashchange',
function(){
console.log('unloading ' + last_hash);
$(window).trigger('unload' + last_hash);
last_hash = location.hash;
console.log('loading ' + location.hash);
$(window).trigger('load' + location.hash);
});
$(window).bind('unload#productsMenu', function() {
$('#productsMenuContent > *').remove();
});
$(window).bind('load#productsMenu',
function() {
console.log('Products menu loaded');
$('<li class="arrow" onclick="activeproduct=1;"><a href="#productClickThru">Strip</a></li>').appendTo($('#productsMenuContent'));
$('<li class="arrow "onclick="activeproduct=2;"><a href="#productClickThru">Prep</a></li>').appendTo($('#productsMenuContent'));
$('<li class="arrow" onclick="activeproduct=3;"><a href="#productClickThru">Heavy Prep</a></li>').appendTo($('#productsMenuContent'));
$('<li class="arrow" onclick="activeproduct=4;"><a href="#productClickThru">Line</a></li>').appendTo($('#productsMenuContent'));
$('<li class="arrow" onclick="activeproduct=5;"><a href="#productClickThru">Finished Paper</a></li>').appendTo($('#productsMenuContent'));
$('<li class="arrow" onclick="activeproduct=6;"><a href="#productClickThru">Emulsion</a></li>').appendTo($('#productsMenuContent'));
$('<li class="arrow" onclick="activeproduct=7;"><a href="#productClickThru">Satin</a></li>').appendTo($('#productsMenuContent'));
});
$(window).bind('load#productClickThru',
function() {
alert(activeproduct);
console.log('Room: '+activeroom);
console.log('Area: '+activearea);
console.log('Product: '+activeproduct);
if( activeproduct == 1 ) {
$('#productClickThru > .toolbar > :header').html('Strip');
$('#productClickThru').find('.room-label').html('Room: '+activeroom);
$('#productClickThru').find('.area-label').html('Area: '+activearea);
} else if( activeproduct == 2 ) {
$('#productClickThru > .toolbar > :header').html('Prep');
$('#productClickThru').find('.room-label').html('Room: '+activeroom);
$('#productClickThru').find('.area-label').html('Area: '+activearea);
} else if( activeproduct == 3 ) {
$('#productClickThru > .toolbar > :header').html('Heavy Prep');
$('#productClickThru').find('.room-label').html('Room: '+activeroom);
$('#productClickThru').find('.area-label').html('Area: '+activearea);
} else if( activeproduct == 4 ) {
$('#productClickThru > .toolbar > :header').html('Line');
$('#productClickThru').find('.room-label').html('Room: '+activeroom);
$('#productClickThru').find('.area-label').html('Area: '+activearea);
} else if( activeproduct == 5 ) {
$('#productClickThru > .toolbar > :header').html('Finished Paper');
$('#productClickThru').find('.room-label').html('Room: '+activeroom);
$('#productClickThru').find('.area-label').html('Area: '+activearea);
} else if( activeproduct == 6 ) {
$('#productClickThru > .toolbar > :header').html('Emulsion');
$('#productClickThru').find('.room-label').html('Room: '+activeroom);
$('#productClickThru').find('.area-label').html('Area: '+activearea);
} else if( activeproduct == 7 ) {
$('#productClickThru > .toolbar > :header').html('Satin');
$('#productClickThru').find('.room-label').html('Room: '+activeroom);
$('#productClickThru').find('.area-label').html('Area: '+activearea);
}
});
});
index.php file:
<div id="productClickThru" class="page">
<div class="toolbar"><h1 name="title"></h1>
<a class="back button" href="#">Back</a>
</div>
<ul>
<li><span class="room-label"></span></li> //gets set by JS
<li><span class="area-label"></span></li> //gets set by JS
<li>
<select id="quantity">
<optgroup label="quantity">
<option value ="10">10</option>
<option value ="20">20</option>
<option value ="20">30</option>
<option value ="20">40</option>
<option value ="20">50</option>
<option value ="20">60</option>
<option value ="20">70</option>
<option value ="20">80</option>
<option value ="20">90</option>
<option value ="20">100</option>
<option value ="20">150</option>
</optgroup>
</select>
</li>
<li><textarea placeholder="Notes" ></textarea></li>
</ul>
<a href="#" class="submit whiteButton">Save</a>
<!-- #new-quote close -->
</div>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可能很需要将 js 包装在自执行函数中,例如:
You may well need to wrap your js in a self executing function such as:
在 js 文件中分配事件处理程序
这也是一种更好的样式(不显眼的 javascript),因为 JS 与 HTML 是分离的。
Assign your event handlers in js file
It would be a better style (unobtrusive javascript) as well, because the JS is separated from HTML.