请参阅第二栏中的对象(Refer to object in second column)

编程入门 行业动态 更新时间:2024-10-26 12:26:53
请参阅第二栏中的对象(Refer to object in second column)

我正在尝试引用第二列中的对象(对象link_icon )并基于.json数据,我正在分配新的url。

我的html结构:

<div class="details"> <h1 id="title">title</h1> <h3 id="author"> <a href="https://www.google.pl/">author</a> </h3> </div> <a href="https://www.google.pl/"> <div><i class="link_icon"></i></div> </a>
$(function(){ var url = 'img_and_data/' + randomNumbers[bgImgIndex] +'.json'; var title = $("#title");// the id of the heading var author = $("#author a");// id of author var link_icon = $(".link_icon a"); // <------- problem probably is here $.getJSON(url, function (data) { title.text(toTitleCase(data.title)); author.text(data.copyright); author.attr("href", data.url); link_icon.attr("href", data.permalink); }); }

这是我的完整代码: https : //jsfiddle.net/cvv0sen8/8/

我应该如何引用link_icon来修改href ?

你可以帮帮我吗?


JSON:

{ "title": "example title", "copyright": "example author", "permalink": "https://www.bing.com/", "url": "http://www.yahoo.com" }

I am trying to refer to object in second column (object link_icon) and based on .json data, I am assigning new url.

Structure of my html:

<div class="details"> <h1 id="title">title</h1> <h3 id="author"> <a href="https://www.google.pl/">author</a> </h3> </div> <a href="https://www.google.pl/"> <div><i class="link_icon"></i></div> </a>
$(function(){ var url = 'img_and_data/' + randomNumbers[bgImgIndex] +'.json'; var title = $("#title");// the id of the heading var author = $("#author a");// id of author var link_icon = $(".link_icon a"); // <------- problem probably is here $.getJSON(url, function (data) { title.text(toTitleCase(data.title)); author.text(data.copyright); author.attr("href", data.url); link_icon.attr("href", data.permalink); }); }

Here is my full code: https://jsfiddle.net/cvv0sen8/8/

How should I refer to link_icon to modify href?

Could you help me?


JSON:

{ "title": "example title", "copyright": "example author", "permalink": "https://www.bing.com/", "url": "http://www.yahoo.com" }

最满意答案

var link_icon = $(“。link_icon a”);

您的选择器在这里的意思是“每个标签都有一个带link_icon作为父级的标签”。 问题是你的html文件在link_icon中没有包含任何标签:你的选择器将不会返回任何内容。

你可以像这样改变你的html:

<div><a class="link_a" href="#"><i class="link_icon"></i></a></div>

而你的js:

var link_a = $(".link_a"); // ... link_a.attr("href", data.permalink);

var link_icon = $(".link_icon a");

Your selector here means "every a tag which has a tag with class link_icon as a parent". Problem is your html document doesn't contain any a tag in link_icon: your selector will return nothing.

You may change your html like this:

<div><a class="link_a" href="#"><i class="link_icon"></i></a></div>

And your js:

var link_a = $(".link_a"); // ... link_a.attr("href", data.permalink);

更多推荐

本文发布于:2023-07-09 14:56:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1087152.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:栏中   对象   Refer   object   column

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!