-1

我正在寻求 Javascript/HTML 中的“更多信息”脚本的帮助。

当前代码:

<div id="information">
    Content
    <div id="more_information" style="display:none;">
        More content
    </div>
<a href="#" onclick="showhide('more_information');">More information</a>​​​​​​​​
</div>

Javascript:

function showhide(layer_ref) {

if (state == 'block') {
state = 'none';
}
else {
state = 'block';
}
if (document.all) { 
eval( "document.all." + layer_ref + ".style.display = state");
}
if (document.layers) {
document.layers[layer_ref].display = state;
}
if (document.getElementById &&!document.all) {
info = document.getElementById(layer_ref);
info.style.display = state;
}
}​

干杯

4

3 回答 3

1

JS:

function showhide(layer_ref) {
  var el = document.getElementById(layer_ref);
  var visible = el.style.display == 'block' // current state
  el.style.display = visible ? 'none' : 'block';
  return !visible; // new state (true=visible,false=invisible)
}

HTML:

<a href="#" onclick="if(showhide('more_information')){this.innerHTML='Less information'}else{this.innerHTML='More information'};return false;">More information</a>​​​​​​​​
于 2012-08-23T12:58:13.110 回答
0

I suggest using jQuery, this makes it a lot easier:

jQuery("#information a").click(function(){
    jQuery(this).parent().find("#more_information").toggle();
});
于 2012-08-23T12:59:39.533 回答
0

只有当您只有一个 id 为“more_information”的 DOM 元素时,它才会起作用:

function showhide(nodeId) {
   var node = document.getElementById(nodeId);
   if (node) {
       node.style.display = node.offsetWidth ? 'none' : 'block';
   }
}​

如果您在页面上几乎没有类似的结构,您应该使用这个:

HTML:

<div id="information">
    Content
    <div id="more_information" style="display:none;">
        More content
    </div>
    <a href="#" onclick="showhide(this); return false;">More information</a>​​​​​​​​
</div>

JS:

function showhide(node) {
   var moreNode = node.previousSibling, i = 5;
   while (moreNode.nodeType != 1 && i--) {
      moreNode = moreNode.previousSibling;
   }

   if (moreNode && moreNode.nodeType == 1) {
       moreNode.style.display = moreNode.offsetWidth ? 'none' : 'block';
   }
}​
于 2012-08-23T13:04:44.317 回答