我有一个看起来像这样的链接:
<a class="link" href="https://www.google.com/?q=">Hello world</a>
是否可以将此渲染为你好,世界?
该链接应指向https://www.google.com/?q=Hello world,但不需要在href中指定。
我认为使用JavaScript可能是最好的方法?
任何帮助都会很好!
我有一个看起来像这样的链接:
<a class="link" href="https://www.google.com/?q=">Hello world</a>
是否可以将此渲染为你好,世界?
该链接应指向https://www.google.com/?q=Hello world,但不需要在href中指定。
我认为使用JavaScript可能是最好的方法?
任何帮助都会很好!
href
总是以格式 https://www.google.com/?q=
出现,您可以执行以下操作:var eles = document.getElementsByTagName('a');
for(var i = 0, len = eles.length; i < len; i++) {
//get the inner html of anchor tag
//var text = eles[i].innerHTML;
//better use textContent
var text = eles[i].textContent;
//append anchor tag inner html to href
eles[i].href += text;
}
eles[i].textContent || eles[i].innerText || ''
。请注意,翻译过程中不会添加解释或其他内容。 - Bob你能试试这个吗,
<a class="link" href="https://www.google.com/?q=" onclick='window.location.href = this.href+encodeURI(this.textContent); return false; '>Hello world</a>
添加 onclick
事件处理程序并获取标签的 href
和 textContent
,将它们组合在一起。然后就会得到期望的结果。
更新:如果您的页面中有很多链接,则可以创建JavaScript函数并在必要时调用它。
Javascript:
function Search(d){
window.location.href = d.href+encodeURI(d.textContent);
return false;
}
HTML:
<a class="link" href="https://www.google.com/?q=" onclick='Search(this);'>Hello world</a>
text = $(".link").html();
link = $(".link").attr('href');
$(".link").attr('href',link+text);
function changeHref () {
var anchor = document.getElementsByTagName('a')[0];
anchor.href += anchor.textContent;
}
<a class="link" onclick="changeHref()" href="https://www.google.com/?q=">Hello world</a>
示例:http://jsbin.com/rudobe/edit?html,js,output
HTML:
<a class="link" href="https://www.google.com/?q=">Hello world</a>
JS:
var anchor = document.getElementsByClassName('link')[0];
var href = anchor.getAttribute('href');
var text = anchor.textContent;
anchor.setAttribute('href', href + text); // The `+` operator concatenates here.
文档:
http://devdocs.io/dom/element.setattribute
http://devdocs.io/dom/element.getattribute
$('.link').click(function(){
$(this).attr('href', this.href + encodeURIComponent($(this).text()));
});
这将在提交链接之前更改href属性
<a class="link" href="#" onclick="navigateToLink(this)">Hello world</a>
在 JavaScript 中,您可以像这样处理它:
function navigateToLink(elt){
console.log("https://www.google.com/?q="+elt.textContent );
window.location.href="https://www.google.com/?q="+ elt.textContent;
}
已经有很多答案了,再来一个也不会有什么影响。假设您没有服务器控制权,否则您只需修改 href 并完成它即可。一个简单的方法是:
<a href="..." onclick="this.href+=this.textContent||this.innerText">...</a>
使用textContent或innerText意味着您可以在链接内容中包含标记,而它不会成为href的一部分。
修改仅具有“google.com” href的链接的更通用解决方案是:
function modifyLinks() {
var link, links = document.links;
var re = /google\.com/i;
for (var i=0, iLen=links.length; i<iLen; i++) {
link = links[i];
if (re.test(link.href)) {
link.href += link.textContent || link.innerText;
}
}
}
window.onload = modifyLinks;
测试可以更改为任何内容,也许您想基于className或其他属性进行测试。如果您想修改所有链接,请删除测试。
试一下这个。
$(document).ready(function(){
$('.link').on('click', function(){
var temp = $(this).attr('href');
var html = $(this).html();
$(this).attr('href',temp+html);
});
});