javascript - 求教为什么这段script改写为函数传参形式就不执行了?
PHPz
PHPz 2017-05-19 10:33:40
0
2
529

js新手,请教为什么写法1改写成传参形式的写法2就不执行了?谢谢!
<html>

    <form id="search-bar" class="hide" action="/action_page.php">

        <input id="search-box" type="search" value="search">
        <input id="search-submit" type="submit">

    </form>
        <button id="search-but" >click me</button>

</html>

写法1:
<script>

    var osearch = document.getElementById("search-but")
    var obar = document.getElementById("search-bar")

    osearch.onclick = function () {

        if (obar.style.display == "block") {
            obar.style.display ="none"
        }
        else {
            obar.style.display="block"
        }
    }

</script>

写法2:
<script>

function display(obj) {

    var b = document.getElementById("obj")

    if (b.style.display == "block") {
        b.style.display = "none"
    }
    else {
        b.style.display = "block"
    }
}

var osearch = document.getElementById("search-but")

 osearch.onclick=display(search-bar);

</script>

PHPz
PHPz

学习是最好的投资!

全部回复(2)
滿天的星座
function display(obj) {

    var b = document.getElementById(obj)

    if (b.style.display == "block") {
        b.style.display = "none"
    }
    else {
        b.style.display = "block"
    }
}

var osearch = document.getElementById("search-but")

 osearch.onclick=function () { display('search-bar'); }

基础不扎实啊

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板