JSON数组href作为jQuery中的一个变量 [英] JSON array href as a variable in jQuery

查看:116
本文介绍了JSON数组href作为jQuery中的一个变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个getJSON函数,将结果转换为html模板,以显示为每个列表项中包含不同超链接的列表。我需要重写链接的正常html函数,因此它不会打开新页面,但会使用位于我的navClickListener()函数中的load()方法加载到特定的分隔线。



如果我将一个虚拟url放入 var gotoURL =''; //< - ..并且将项目链接到此VAR ... ,它按计划工作,但我需要从数组项目中抓取实际的url。



我无法想出一个办法来做到这一点。你可以看看我的代码片段,并注意所有CAPS中的内嵌评论。



感谢您的帮助。

 函数loadNav(url,container,appendE){
$ .getJSON(url,function(data){

$ .each (data.items,function(){
var newItem = $('#'+ container).clone();
//现在用数据
...填写字段。 。
newItem.find(a)。attr(href,this.gotoURL); //< - PREVENT NORMAL FUNCTION ...
//将新的列表项添加到页面
newItem.children()。appendTo('#'+ appendE)
});
。。。
//点击导航项的监听器
var target = data.targetKey;
var gotoURL =''; //< - ..并获取这个项目链接到这个VAR ...
navClickListener(appendE,target,gotoURL);
});
};
$ b $ / * ==========导航点击侦听器函数============= * /
函数navClickListener(appendE,target, ('#'+ appendE).on('click','a',function(e){
e.preventDefault();
$('#' + target).load(gotoURL); //< --...因此它进入了这里??
。。。。
});
};


解决方案

执行此操作的最佳方法是找到href中的内联JavaScript需要将数据库中的gotoURL行更改为: javascript:navClickListener('bodyContent','http://192.168.1.34/wiki/index.php/Airworthiness_Directive#content' ); > newItem.find(a)。attr(href,this.gotoURL)在内部保持双引号的重要性。 ; 在 jQuery.getJSON(url [,data] [,success(data,textStatus,jqXHR)])函数中执行。任何其他类型的内联javaScript方法都不行! (即: javascript:void(0);onclick ='javascript:navClickListener(navContent,bodyContenthttp://192.168.1.34/wiki/index.php/Airworthiness_Directive#content );')< - 查看 onClick 前的结束引用。





删除:

  var gotoURL =''; // <  -  ..并将项目链接到此VAR ... 
navClickListener(appendE,target,gotoURL);

将navClickListener()函数更改为:

$ p $函数navClickListener(target,gotoURL){
$('#'+ target).load(gotoURL);
};

在gotoURL中更改了数据库数据:

  /wiki/index.php/Airworthiness_Directive #content 

到:

  javascript: navClickListener('bodyContent','http://192.168.1.34/wiki/index.php/Airworthiness_Directive#content'); 


I have a getJSON function that converts the results to an html template to display as a list that contains a different hyperlink within each list item. I need to override the normal html function of the link so it doesn't open a new page but loads into a specific divide using the load() method located within my navClickListener() function.

If I put a dummy url into var gotoURL = '';//<--..AND GET THE ITEMS LINK INTO THIS VAR..., it works as planned but I need to grab the actual url's from the array items.

I cannot figure a way to do this. Can you please look thru my snippets and pay attention to the inline comments in all CAPS.

Thank-you for your help.

function loadNav(url, container, appendE) {
        $.getJSON(url, function(data){

            $.each(data.items, function(){
                var newItem = $('#' + container).clone();
                // Now fill in the fields with the data
                . . . .
                newItem.find("a").attr("href", this.gotoURL);//<--PREVENT NORMAL FUNCTION...
                // And add the new list item to the page
                newItem.children().appendTo('#' + appendE)
            });
            . . . .
            // Click listener for navigation items
            var target = data.targetKey;
            var gotoURL = '';//<--..AND GET THE ITEMS LINK INTO THIS VAR...
            navClickListener(appendE, target, gotoURL);
        });
    };

    /* ========== Navigation click listener function ============= */   
    function navClickListener(appendE, target, gotoURL) {
        $('#' + appendE).on('click', 'a', function(e){
            e.preventDefault();
             $('#' + target).load(gotoURL);//<--...SO IT GETS INTO HERE??
             . . . .
        });
    };

解决方案

The best way to do this I found was to do an inline JavaScript in the href witch required changing the gotoURL row in the database to: javascript:navClickListener('bodyContent', 'http://192.168.1.34/wiki/index.php/Airworthiness_Directive #content'); This is important that its inline to maintain the double-quotes at the ends when newItem.find("a").attr("href", this.gotoURL); is executed in a jQuery.getJSON( url [, data] [, success(data, textStatus, jqXHR)] ) function. Any other type of methode of inline javaScript will not work!! (ie: javascript:void(0);" onclick='javascript:navClickListener("navContent", "bodyContent" "http://192.168.1.34/wiki/index.php/Airworthiness_Directive #content");') <--See the end-quote before onClick.

Additional script changes from above:

REMOVED:

var gotoURL = '';//<--..AND GET THE ITEMS LINK INTO THIS VAR...
navClickListener(appendE, target, gotoURL);

Changed the navClickListener() function to:

function navClickListener(target, gotoURL) {
    $('#' + target).load(gotoURL);
};

Changed database data in gotoURL from:

/wiki/index.php/Airworthiness_Directive #content

to:

javascript:navClickListener('bodyContent', 'http://192.168.1.34/wiki/index.php/Airworthiness_Directive #content');

这篇关于JSON数组href作为jQuery中的一个变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆