te')); return $arr; } /* 遍历用户所有主题 * @param $uid 用户ID * @param int $page 页数 * @param int $pagesize 每页记录条数 * @param bool $desc 排序方式 TRUE降序 FALSE升序 * @param string $key 返回的数组用那一列的值作为 key * @param array $col 查询哪些列 */ function thread_tid_find_by_uid($uid, $page = 1, $pagesize = 1000, $desc = TRUE, $key = 'tid', $col = array()) { if (empty($uid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('uid' => $uid), array('tid' => $orderby), $page, $pagesize, $key, $col); return $arr; } // 遍历栏目下tid 支持数组 $fid = array(1,2,3) function thread_tid_find_by_fid($fid, $page = 1, $pagesize = 1000, $desc = TRUE) { if (empty($fid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('fid' => $fid), array('tid' => $orderby), $page, $pagesize, 'tid', array('tid', 'verify_date')); return $arr; } function thread_tid_delete($tid) { if (empty($tid)) return FALSE; $r = thread_tid__delete(array('tid' => $tid)); return $r; } function thread_tid_count() { $n = thread_tid__count(); return $n; } // 统计用户主题数 大数量下严谨使用非主键统计 function thread_uid_count($uid) { $n = thread_tid__count(array('uid' => $uid)); return $n; } // 统计栏目主题数 大数量下严谨使用非主键统计 function thread_fid_count($fid) { $n = thread_tid__count(array('fid' => $fid)); return $n; } ?>css - Javascript: style.display:'block' doesn't work - Stack Overflow
最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

css - Javascript: style.display:'block' doesn't work - Stack Overflow

programmeradmin3浏览0评论

any idea why this JavaScript just shows "block" and not the content of the (hidden) DIV?

<html>
<body>

<div id="mydiv" style="display:none">TEST</div>

<a href="javascript:document.getElementById('mydiv').style.display='block';">Show my DIV</a>

</body>
</html>

I also tried 'inline' but with the same result.

return false/true also failed.

onclick='' also failed.

I know there is style.visibility etc. but i need none/block.

Also the function should work inside the link, i don't want to call an external JS-function.

Thanks!

any idea why this JavaScript just shows "block" and not the content of the (hidden) DIV?

<html>
<body>

<div id="mydiv" style="display:none">TEST</div>

<a href="javascript:document.getElementById('mydiv').style.display='block';">Show my DIV</a>

</body>
</html>

I also tried 'inline' but with the same result.

return false/true also failed.

onclick='' also failed.

I know there is style.visibility etc. but i need none/block.

Also the function should work inside the link, i don't want to call an external JS-function.

Thanks!

Share Improve this question asked May 25, 2014 at 19:55 Roman TischRoman Tisch 2173 gold badges4 silver badges13 bronze badges 0
Add a ment  | 

5 Answers 5

Reset to default 5

Because you want to use an onclick event handler, not the href attribute:

<a href="#" onclick="document.getElementById('mydiv').style.display='block';">Show my DIV</a>

jsFiddle example

(side note: inline JavaScript is usually frowned upon)

The value of the javascript expression

document.getElementById('mydiv').style.display='block'

is the string 'block' - think of the way you can do a=b='something' - in javascript the value of an assignment expression is the assigned value.

If you try

<a href="javascript:'howdy'">link</a>

You'll find clicking the link navigates to a document containing just the word howdy - and the same thing is happening with your code. You can stop this happening by adding an explicit void(0), or by wrapping the code in an immediately-invoked function expression that doesn't return a value (i.e. implicitly returns undefined), so:

<a href="javascript:(function(){document.getElementById('mydiv').style.display='block';})()">

(This structure is monly used in bookmarklets). However, as several ments have already pointed out, in general the use of javascript: hrefs is frowned upon, and you should consider using event handling instead.

This code describes your problem: http://jsbin./jigiy/1

<div id="mydiv" style="display:none">TEST</div>
<a href="javascript:document.getElementById('mydiv').style.display='block',false;">Show my DIV</a>

Why is this happening:

Javascript setters return the value input, so document.getElementById('mydiv').style.display='block' will return 'block', which is equal to href="javascript:'block'". Now it refers to about:blank and sets its content to block.

I'm not sure why the browsers refer and set the content of about:blank, but i think this has something to do with data-urls.

Test this:

<html>
<body>

<div id="mydiv" style="display:none">TEST</div>

<span onclick="document.getElementById('mydiv').style.display = 'block'">Show my DIV</span>

</body>
</html>

-> my problem was using this method to display html tag:

const helloDiv = document.getElementById('hello')
helloDiv.style.display = "block"

-> the solution was to replace html tag:

document.getElementById('hello').style.display = "block"

and it worked for me

发布评论

评论列表(0)

  1. 暂无评论