【问题标题】:jQuery to trigger event through query stringjQuery通过查询字符串触发事件
【发布时间】:2010-07-08 16:47:20
【问题描述】:

我有两个独立的 jQuery 函数,允许用户切换 div 的可见性。默认情况下,div 将处于关闭状态。我正在尝试找出一种方法来设置指向此页面的链接,该链接将根据传递给查询字符串的 ID 打开特定的 div。我的 jQuery 看起来像这样:

    jQuery(document).ready(function(){

   //make first div always open

    jQuery(".toggle_container:not(:first)").hide(); 
    jQuery(".toggle_container:first").show();
    jQuery("h6.trigger:first").addClass("active");

    jQuery("h6.trigger").click(function(){
      jQuery(this).toggleClass("active");
      jQuery(this).next(".toggle_container").slideToggle(300);
    });

    //The second function

    jQuery(function(){
        jQuery(".toggleIt").click(function(){
          jQuery(this).next(".hiddenText").slideToggle("fast"); 
            jQuery(this).html(function(i,html) {
                if (html.indexOf('+') != -1 ){
                   html = html.replace('+','-');
                } else {
                   html = html.replace('-','+');
                }
                return html;
            })
        });
    }); 

HTML

<h6 class="trigger" id='uniqueID"></h6>
 <div class="toggle_container">
TEST
</div>

//and the second toggled container
 <p class="toggleThis tips" id="AnotherID">+</p>
<div class="hiddenText2">
Another TEST
</div>

我想在 url 的查询字符串中添加一个 ID,并让它打开具有相同 ID 的隐藏 div。到目前为止,我的结构是这样的......

var category = getParameterByName('cat');
var id = getParameterByName('id');

if(id)
{

}
else if(category)
{

}
else
{
    //moving this in here 
    jQuery(".toggle_container:not(:first)").hide(); 
    jQuery(".toggle_container:first").show();
    jQuery("h6.trigger:first").addClass("active");
}

我在这部分卡住了。有小费吗?

【问题讨论】:

    标签: jquery string events


    【解决方案1】:

    您是使用 PHP 还是其他语言来创建 HTML?.. 我会创建一个隐藏字段,填充查询变量,如果该字段已填充,则使用 onLoad 打开 div。

    【讨论】:

    • 确实需要通过查询字符串来传递。
    • 是的,我同意...您通过查询字符串传递它。您的页面创建语言将其作为值嵌入到 HTML 输入元素中。 jQuery 拾取并对其进行操作。
    • 也许我理解错了。不过,我确实需要将其保留在前端。我相当确定有一种方法可以触发点击事件,或者使用 hash + var 来触发它。只是无法确定。不过谢谢!
    • 尝试查看其他 SO 问题:stackoverflow.com/questions/439463/…
    猜你喜欢
    • 2015-04-17
    • 1970-01-01
    • 2015-09-01
    • 1970-01-01
    • 2018-12-12
    • 1970-01-01
    • 1970-01-01
    • 2016-04-06
    • 1970-01-01
    相关资源
    最近更新 更多