[英]Nesting in javascript
I don't know if that title even describes what I want to do, but here goes. 我不知道该标题是否描述了我想做的事情,但是这里有。
I have some JavaScript, which changes the href of a link, depending on what item is selected in a select box. 我有一些JavaScript,它会根据选择框中选择的项目来更改链接的href。 Here's the code:
这是代码:
function setText(text) {
var selectVal = text;
var url = $('twitter2').attr("href");
url = 'https://twitter.com/intent/tweet?button_hashtag=stream&text=Just enjoying ' + selectVal + ' on';
$('a').attr("href", url);
}
The problem is there are multiple links on the page. 问题是页面上有多个链接。 Would there be a way to specify which link to change the URL of?
是否可以指定更改URL的链接? Sorry for the woeful explanation, here's the accompanying html.
抱歉,我们提供的html令人毛骨悚然。
<twitter2><a><img src="http://stream.cz.cc/images/twitter.png" onmouseover="this.src='http://stream.cz.cc/images/twitterover.png'" onmouseout="this.src='http://stream.cz.cc/images/twitter.png'" /></a></twitter2>
Any advice? 有什么建议吗?
Add an id to your image tag and JQuery select it by the ID. 在您的图片标签中添加一个ID,然后JQuery通过该ID将其选中。
<img id="THISONE" src="http://stream.cz.cc/images/twitter.png" onmouseover="this.src='http://stream.cz.cc/images/twitterover.png'" onmouseout="this.src='http://stream.cz.cc/images/twitter.png'" /></a>
Javascript: 使用Javascript:
function setText(text) {
var selectVal = text;
var url = $('twitter2').attr("href");
url = 'https://twitter.com/intent/tweet?button_hashtag=stream&text=Just enjoying ' + selectVal + ' on';
$('#THISONE').attr("href", url);
}
first of all 首先
var url = $('twitter2').attr("href")
would give you nothing, as twitter2
does not have any href
attribute. var url = $('twitter2').attr("href")
不会给您任何twitter2
,因为twitter2
没有任何href
属性。
second, you can access your a as: 其次,您可以通过以下方式访问您的a:
url = 'https://twitter.com/intent/tweet?button_hashtag=stream&text=Just enjoying ' + selectVal + ' on';
$('twitter2 a').first().attr("href", url).
This will change the href
of the first a inside the <twitter2>
tag 这将更改
<twitter2>
标记内第一个a的href
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.