javascript 如何在 jQuery 数据表上添加带有搜索图标的占位符文本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34152195/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 17:19:28  来源:igfitidea点击:

How to add placeholder text with search icon on jQuery data tables

javascriptjquerycssdatatables

提问by Sanduni Di

I'm using jQuery data tables and I want to display "search" text with search icon inside search input box as placeholder. I managed add the text. Please advice me how to add placeholder image

我正在使用 jQuery 数据表,我想在搜索输入框中显示带有搜索图标的“搜索”文本作为占位符。我设法添加了文本。请建议我如何添加占位符图像

$(document).ready(function() {
        $('#example').DataTable({
            oLanguage: {
                sSearch: ''
            },
            "aoColumnDefs":[{
                'bSortable':false, 'aTargets':[3]

            }
            ]
        });

        function InitComplete(oSettings) {
            $('#example_filter')
                    .contents()
                    .filter(function() { return this.nodeType == 3 })
                    .replaceWith('Refine search: ');
        }

        $('.dataTables_filter input').attr("placeholder", "Search");

I need to know, how to add image something like '<i class='icon-search'></i>'

我需要知道,如何添加图像 '<i class='icon-search'></i>'

采纳答案by P. Frank

For make that, you can use background-image. try

为此,您可以使用background-image. 尝试

UPDATE

更新

$(document).ready(function() {
  
    $(".searchIn").keypress(function(){
      $(this).removeClass().addClass("searchOut")
    })
    
    $(".searchIn").click(function(){
      if(!$(this).hasClass("searchOut"))
        $(this).addClass("searchIn")
    })
    
    $(document).on("keyup",".searchOut", function(){
      if(($(this).val().length) == 0 )
        $(this).removeClass().addClass("searchIn")
    })
})
.searchIn{
  background:url(https://cdn0.iconfinder.com/data/icons/basic-website/512/search-website-512.png) no-repeat scroll left center / 15px auto;
}

.searchOut{
background:none; 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="main">
  <input type="text" class="searchIn"  />
</div>

回答by T?i B?i Mít T?

Add

添加

"searchPlaceholder": "search"

Ex

前任

$('#datatable').DataTable({
        "dom": 'lCfrtip',
            "order": [],
            "colVis": {
            "buttonText": "Hi?n th?",
                "overlayFade": 0,
                "align": "right"
        },
        "language": {
            "lengthMenu": '_MENU_ b?n ghi trên trang',
                "search": '<i class="fa fa-search"></i>',
                "searchPlaceholder": "search",
                "paginate": {
                "previous": '<i class="fa fa-angle-left"></i>',
                    "next": '<i class="fa fa-angle-right"></i>'
            }
        }
    });

回答by Sarah Aziziyan

you can use .css() in jquery:

你可以在 jquery 中使用 .css() :

$(".dataTables_filter input").css("background-image", "url('yourImageURL')");

but as you mentioned that you want to use an icon, you can use an input field with an <i>tag. using CSS you can set the position of the icon in a place that the user would think it's on the <input>tag:

但正如您提到要使用图标,您可以使用带有<i>标签的输入字段。使用 CSS,您可以将图标的位置设置在用户认为它在<input>标签上的位置:

the HTML could look like this :

HTML 可能如下所示:

<input type="text" id="username">
<i class='icon-search'></i>

then you have to give the icon-search a position where it can be seen inside of the input depending on where the <input>tag is positioned. sth like this:

那么你必须给图标搜索一个可以在输入中看到的位置,这取决于<input>标签的位置。像这样:

.icon-search {
    left: 0px;
    top: 0px;
    position: absolute;
    height: 36px;
    width: 36px;    
    text-align: center;  
}

回答by aries

You can write this code to add a placeholder text for the search in jQuery DataTable $('#user-notification-table_filter input').attr("placeholder", "Search for users");

您可以编写此代码为 jQuery DataTable 中的搜索添加占位符文本$('#user-notification-table_filter input').attr("placeholder", "Search for users");