简体   繁体   English

从 jQuery 选项卡式搜索脚本中删除链接 ID 元素

[英]Removing link ID element from jQuery tabbed search script

I have a jQuery tabbed search script that gets content from a PHP file defined by the link and parses it to the results div element.我有一个 jQuery 选项卡式搜索脚本,它从链接定义的 PHP 文件中获取内容并将其解析为结果 div 元素。 The ID for each link is used to pull content from the correct file however type_ is needed in the link ID for the tabs to work which then doesn't pull content from the right place.每个链接的 ID 用于从正确的文件中提取内容,但是在链接 ID 中需要 type_ 才能使选项卡正常工作,这样就不会从正确的位置提取内容。 How can I resolve this issue?我该如何解决这个问题?

This is my current jQuery code:这是我当前的 jQuery 代码:

$(document).ready(function(){
    $("[id^=type_]").click(function(){
        type=$(this).attr("id");
        $("[id^=type_]").removeClass("selected");
        $("#"+type).addClass("selected");
        return false;
    });
    $("#type_tab1").click();
    $("#query").keyup(function(){
            var query=$(this).val();
            var yt_url=''+type+'.php?q='+query;
            if(query==''){
                  window.location.hash='';
                  document.title='My Search Script';
            }
            $.ajax({
                type:"GET",
                url:yt_url,
                dataType:"html",
                success:function(results){
                   $('#results').html(results);
                }
            });
    });
});

This is my HTML code:这是我的 HTML 代码:

<ul> 
<li><a id="type_tab1" href="javascript:void(null);">Tab1</a></li> 
<li><a id="type_tab2" href="javascript:void(null);">Tab2</a></li> 
<li><a id="type_tab3" href="javascript:void(null);">Tab3</a></li> 
</ul>

If I've understood your question correctly, you want to use only the part after the underscore character when you send your GET request.如果我正确理解了您的问题,那么您在发送 GET 请求时只想使用下划线字符之后的部分。

If that's the case, you can do this in your click event hander:如果是这种情况,您可以在单击事件处理程序中执行此操作:

type=$(this).attr("id").replace("type_", "");

Note that you can simplify this, because you don't need to use jQuery to get the id attribute:请注意,您可以简化这一点,因为您不需要使用 jQuery 来获取id属性:

type=this.id.replace("type_", "");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM