从论坛看了段代码:
<input type="button" value="ShowDirection" onclick="ShowDirection(this)"/> function ShowDirection(btn) {
var tbl = [];
var direct = (document.selection && document.selection.createRange)
? document.selection.createRange().parentElement() // IE
: window.getSelection().focusNode.parentNode; //FF
.....................
}
当打开页面或者刷新页面后,鼠标不在页面有任何点击之前,就开始点击ShowDirection按钮,将会提示:
Uncaught TypeError: Cannot read property 'parentNode' of null 请问该如何解决呢?
if(鼠标没有点击页面就开始点击这个按钮){提示: }谢谢
<input type="button" value="ShowDirection" onclick="ShowDirection(this)"/> function ShowDirection(btn) {
var tbl = [];
var direct = (document.selection && document.selection.createRange)
? document.selection.createRange().parentElement() // IE
: window.getSelection().focusNode.parentNode; //FF
.....................
}
当打开页面或者刷新页面后,鼠标不在页面有任何点击之前,就开始点击ShowDirection按钮,将会提示:
Uncaught TypeError: Cannot read property 'parentNode' of null 请问该如何解决呢?
if(鼠标没有点击页面就开始点击这个按钮){提示: }谢谢
window.getSelection().focusNode.parentNode: null;
这样单独用是行的,但继续运行下面的会出现:
Uncaught TypeError: Cannot read property 'tagName' of null
谢谢
<input type="button" value="ShowDirection" onclick="ShowDirection(this)"/>
function ShowDirection(btn) {
var tbl = [];
var direct = (document.selection && document.selection.createRange)
? document.selection.createRange().parentElement() // IE
: window.getSelection().focusNode? window.getSelection().focusNode.parentNode: null;//FF
do
{
tbl.push(direct.tagName);
}
while((direct = direct.parentNode) && (direct !== document.documentElement));
alert(tbl.reverse().join('---->'))
}
Uncaught TypeError: Cannot read property 'tagName' of null 请问该如何解决呢?
if(鼠标没有点击页面就开始点击这个按钮){提示: }