在 Blackberry 手机上使用 JavaScript 操作 DOM
这是一段代码(来自一个更大的代码),在黑莓中不起作用。我正在操作 DOM 以动态插入 ul 元素。
我正在 Blackberry 9700、OS 5.0 上进行测试。我正在做的事情非常基本,无需费脑筋,并且可以在其他桌面浏览器上运行。我知道 Blackberry 上的 JavaScript 支持并不完善——但是这些基本的东西呢?
附件是我看到的错误的屏幕截图。 JavaScript 报告函数 insertBefore 不存在。但是,我已经测试过它存在并且当我直接附加到正文时
document.body.insertBefore( newNode, sibling );
可以工作不起作用的代码是:
<html>
<head>
<script type="text/javascript">
function toggle(){
_ul = document.getElementById('ul-list');
if( _ul )
{
document.body.removeChild( _ul );
}else
{
ul = document.createElement( "ul" );
ul.setAttribute('id','ul-list');
li = document.createElement( 'li' );
li.appendChild( document.createTextNode("HTML") );
ul.appendChild( li );
li = document.createElement( 'li' );
li.appendChild( document.createTextNode("CSS") );
ul.appendChild( li );
li = document.createElement( 'li' );
li.appendChild( document.createTextNode("JavaScript") );
ul.appendChild( li );
li = document.createElement( 'li' );
li.appendChild( document.createTextNode("XML") );
ul.appendChild( li );
parent = document.getElementById('container');
sibling = document.getElementById('para');
try
{
func = parent.insertBefore;
if( !func )
{
alert( "Function doesn't exist!" );
}
parent.insertBefore( ul, sibling );
}catch( err )
{
alert( err );
}
}
}
</script>
<style type="text/css">
</style>
</head>
<body>
<div id ="container">
<p id ="para" >Display this link list as a horizontal menu:</p>
<input type="button" name="Show Now!" value ="Toggle" onClick=" toggle();"/>
</div>
</body>
</html>
Here is a snippet of code (from a much larger code) that doesn't work in Blackberry. I am manipulating DOM to insert a ul element dynamically.
I am testing on Blackberry 9700, OS 5.0. What I am doing is very basic, no brainer and works on other desktop browsers. I know JavaScript support on Blackberry is patchy - but this basic stuff?
Attached is the screen shot of the error I see. JavaScript reports that the function insertBefore doesn't exist. However, I have tested that it exists and works when I append directly to the body
document.body.insertBefore( newNode, sibling );
The code that doesn't work is :
<html>
<head>
<script type="text/javascript">
function toggle(){
_ul = document.getElementById('ul-list');
if( _ul )
{
document.body.removeChild( _ul );
}else
{
ul = document.createElement( "ul" );
ul.setAttribute('id','ul-list');
li = document.createElement( 'li' );
li.appendChild( document.createTextNode("HTML") );
ul.appendChild( li );
li = document.createElement( 'li' );
li.appendChild( document.createTextNode("CSS") );
ul.appendChild( li );
li = document.createElement( 'li' );
li.appendChild( document.createTextNode("JavaScript") );
ul.appendChild( li );
li = document.createElement( 'li' );
li.appendChild( document.createTextNode("XML") );
ul.appendChild( li );
parent = document.getElementById('container');
sibling = document.getElementById('para');
try
{
func = parent.insertBefore;
if( !func )
{
alert( "Function doesn't exist!" );
}
parent.insertBefore( ul, sibling );
}catch( err )
{
alert( err );
}
}
}
</script>
<style type="text/css">
</style>
</head>
<body>
<div id ="container">
<p id ="para" >Display this link list as a horizontal menu:</p>
<input type="button" name="Show Now!" value ="Toggle" onClick=" toggle();"/>
</div>
</body>
</html>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我确认这是 Blackberry 5.0(在设备 9000 和 9700 上)中 DOM 实现中的一个错误。
解决方法是,如果必须动态插入或删除元素,则将元素直接放置在 body 下方。有效的代码是:
更改是内容直接位于 body 下,而不是 div“容器”的子级。我相信这在使用WebKit渲染引擎的Blackberry 6.0+中不是问题。将需要忍受这个有缺陷的浏览器,直到不需要支持为止。
I confirm that this is a bug in implementation of DOM in Blackberry 5.0 (on devices 9000 and 9700).
The work around is to have the elements placed directly under body if they have to be dynamically inserted or removed. The code that works is:
The change is the content is directly under body and not child of div "container". I believe this is not an issue in Blackberry 6.0+, where WebKit rendering engine is used. Will need to live with this buggy browser till the time they need not be supported.