如何在javascript中使用href

时间:2018-01-11 03:56:57

标签: javascript jquery html

我正在我的网络应用程序中创建一个搜索选项,我这样编码

<div class="form-group form-group-search" style="margin-top:1.5em;">
    <div class="input-group" style="width:92%; margin:auto">
        <input id="txt-search" type="text" class="form-control" placeholder="Search Keyword..." />
        <span class="input-group-btn">
            <a class="btn btn-default" id ="btn-search" data-toggle="tab" href="#div-search">
            <span class="fa fa-search"></span></a>
        </span>
    </div>
</div>

我显示的div取决于链接的href

<div id="div-search" class="tab-pane fade">
    <section class="content-header">
        <h1><i class="fa fa-search"></i><span style="margin-left:.3em;">Result</span>
        </h1>
    </section>
    <section class="content container-fluid">
        <div class="col-sm-3">
            <span> search </span>
        </div>
    </section>
</div>
<div id="div-home" class="tab-pane fade">
    <section class="content-header">
        <h1><i class="fa fa-home"></i><span style="margin-left:.2em;">Home</span>
        </h1>
    </section>

    <section class="content container-fluid">
        <div class="row">
            <div class="col-sm-12" style="text-align:center;">
                <div style="border-top:2px solid #555555; width:70%; margin:auto;"></div>
            </div>
        </div>
        <div class="row" style="margin-top:1em;">
            <div class="col-sm-12" style="text-align:center;">
            Description
            </div>          
        </div>
    </section>
</div>

但我要做的是,如果#txt-search为空或为空,或“”div-search将不会显示。它什么都不做。我试过window.location,但这不是我想要的。

理想情况下,如果#txt-search不等于“”,它将无效。

$(document).on("click", "#btn-search", function () {

    if ($("#txt-search").val() != ""){
        $(".sidebar-menu > li").removeClass("active");
        $(".sidebar-menu > li:nth-child(4)").removeClass("menu-open");
        $(".treeview-menu").slideUp();
    } 
    else{
    }
});

3 个答案:

答案 0 :(得分:1)

你可以这样做。使用hide()show()功能

$(function(){
    $('#btn-search').click(function(){
        var input = $('#txt-search').val();
        if(typeof input === 'undefined' || input == null || input == ''){
            $('#div-search').hide();
        } else {
            $('#div-search').show();
            // You want...
        }
    })  
});

答案 1 :(得分:1)

您可以在Jquery

中使用hide()show()隐藏和显示div
$('#btn-search').click(function(){
  if($("#txt-search").val() !== ""){
    $('#div-search').hide();
  } else {
    $('#div-search').show()
  }
});

答案 2 :(得分:1)

你可以尝试这样的事情。请查看以下代码:

更改gdtools

gdtools

<强>的jQuery

> install.packages("flextable")

  There is a binary version available but the source version is later:
          binary source needs_compilation
flextable  0.4.0  0.4.2             FALSE

installing the source package ‘flextable’

trying URL 'https://cran.rstudio.com/src/contrib/flextable_0.4.2.tar.gz'
Content type 'application/x-gzip' length 135276 bytes (132 KB)
==================================================
downloaded 132 KB

Warning in strptime(xx, f <- "%Y-%m-%d %H:%M:%OS", tz = tz) :
  unknown timezone 'default/Australia/Melbourne'
* installing *source* package ‘flextable’ ...
** package ‘flextable’ successfully unpacked and MD5 sums checked
** R
** inst
** preparing package for lazy loading
Error in dyn.load(file, DLLpath = DLLpath, ...) : 
  unable to load shared object '/Library/Frameworks/R.framework/Versions/3.4/Resources/library/gdtools/libs/gdtools.so':
  dlopen(/Library/Frameworks/R.framework/Versions/3.4/Resources/library/gdtools/libs/gdtools.so, 6): Library not loaded: /opt/X11/lib/libcairo.2.dylib
  Referenced from: /Library/Frameworks/R.framework/Versions/3.4/Resources/library/gdtools/libs/gdtools.so
  Reason: image not found
ERROR: lazy loading failed for package ‘flextable’
* removing ‘/Library/Frameworks/R.framework/Versions/3.4/Resources/library/flextable’
Warning in install.packages :
  installation of package ‘flextable’ had non-zero exit status

The downloaded source packages are in
    ‘/private/var/folders/fs/2zzbjym545vgl0hvq4pvs6sc0000gn/T/Rtmpdo3FMp/downloaded_packages’