简体   繁体   English

jQuery-附加2个对象以创建可点击的链接

[英]Jquery - attach 2 objects to make a clickable link

Sorry in advance as I know how basic this question is but I'm struggling to find the answer to this. 预先抱歉,因为我知道这个问题的基础,但是我一直在努力寻找答案。

I'm trying to connect two objects together, some text & a url and send them to a class. 我正在尝试将两个对象(一些文本和一个URL)连接在一起,然后将它们发送给一个类。

The code I have that's working just fine is only using one of the objects at the moment; 我拥有的可以正常工作的代码目前仅使用其中一个对象。

$('.myclass').text(obj.mytext);

I'm stuck on how to attach the second object (obj.myurl) to this so it output's the text as a clickable link? 我一直在坚持如何将第二个对象(obj.myurl)附加到此,以便将文本输出为可点击的链接?

尝试:

$('.myclass').text(obj.mytext).attr('href',obj.myurl);

You shoule 你应该

$('.myclass').html('<a href="' + obj.myurl + '">' + obj.mytext + '</a>');

or 要么

$('<a/>', { href : obj.myurl, text: obj.mytext }).appendTo('.myclass')

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

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