JS中怎么定位焦点

当对A进行操作时,先判断B是不是为空,如果B为空的话,提示“选择A时先要选择B”然后把焦点就指向B。这段代码怎么写。

<body onload="javascript:showLoginMeg('usrId','${loginForm.loginMsg}');">  //在body中定义事件  
function showLoginMeg(logobj,loginMsg){
        if ("" != loginMsg){
         //logobj.focus();   
         alert(loginMsg);
         document.getElementById(logobj).focus();  //此处的logobj是一个字符串 得把他转换成对象进行焦点的定位
        }
    }


<pre></pre>
<pre></pre>
<div class="translator-theme-default" id="translator-floating-panel" style="bottom:auto; left:0px; right:auto; top:89px">
<div title="点击翻译" id="translator-floating-panel-button"></div>
</div>
<pre></pre>
<div class="translator-theme-default" id="translator-floating-panel" style="bottom:auto; left:545px; right:auto; top:20px">
<div title="点击翻译" id="translator-floating-panel-button"></div>
</div>
    
        <div style="padding-top:20px">         
            <p style="font-size:12px;">版权声明:本文为博主原创文章,未经博主允许不得转载。</p>
        </div>

温馨提示:答案为网友推荐,仅供参考
第1个回答  推荐于2017-09-22
假设html结构如下:
<input type="radio" id="r1" value="A">A <input type="radio" id="r2" value="B">B
jquery脚本:
var a = $("#r1");
var b = $("#r2");
a.click(function(){
if(b.attr("checked") == '') alert("请选择b");

});本回答被提问者和网友采纳
第2个回答  2012-10-30
document.getElementById('b').focus();