Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/443.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript HTML在元素上添加属性_Javascript_Html - Fatal编程技术网

Javascript HTML在元素上添加属性

Javascript HTML在元素上添加属性,javascript,html,Javascript,Html,当我写(很多)标记时,我没有写'target=“\u blank”,因此没有链接指向其他窗口或标记 有没有办法将“target=''u blank'添加到使用JavaScript的所有链接中?前面的回答如下: 代码: /* here are two different ways to do this */ //using jquery: $(document).ready(function(){ $('#link_other a').attr('target', '_blank'); });

当我写(很多)标记时,我没有写
'target=“\u blank”
,因此没有链接指向其他窗口或标记


有没有办法将
“target=''u blank'
添加到使用JavaScript的所有链接中?

前面的回答如下:

代码:

/* here are two different ways to do this */
//using jquery:
$(document).ready(function(){
  $('#link_other a').attr('target', '_blank');
});

// not using jquery
window.onload = function(){
  var anchors = document.getElementById('link_other').getElementsByTagName('a');
  for (var i=0; i<anchors.length; i++){
    anchors[i].setAttribute('target', '_blank');
  }
}
// jquery is prettier. :-)
/*这里有两种不同的方法*/
//使用jquery:
$(文档).ready(函数(){
$('#link_other a').attr('target','u blank');
});
//不使用jquery
window.onload=函数(){
var anchors=document.getElementById('link_other').getElementsByTagName('a');

对于(var i=0;i之前在此处回答:

代码:

/* here are two different ways to do this */
//using jquery:
$(document).ready(function(){
  $('#link_other a').attr('target', '_blank');
});

// not using jquery
window.onload = function(){
  var anchors = document.getElementById('link_other').getElementsByTagName('a');
  for (var i=0; i<anchors.length; i++){
    anchors[i].setAttribute('target', '_blank');
  }
}
// jquery is prettier. :-)
/*这里有两种不同的方法*/
//使用jquery:
$(文档).ready(函数(){
$('#link_other a').attr('target','u blank');
});
//不使用jquery
window.onload=函数(){
var anchors=document.getElementById('link_other').getElementsByTagName('a');

对于(var i=0;i您根本不需要JavaScript。您可以使用
head
中的
base
元素为锚定指定基本URL或目标

将在新窗口和/或选项卡中打开页面上的所有链接


有关基本元素的更多信息,请访问。

您根本不需要JavaScript。您可以使用
标题中的
base
元素指定锚定的基本URL或目标

将在新窗口和/或选项卡中打开页面上的所有链接


有关基本元素的更多信息可以在上找到。

切题旁,但仅仅修改标记是否更有意义?您是否试图避免搜索和替换?是的,在我的网站中有很多链接,很难在所有链接中键入它。切题旁,但仅仅修改标记是否更有意义你想避免搜索和替换吗?是的,在我的网站上有很多链接,很难在所有链接中键入。谢谢!!!成功了。因为我不能在付费的地方学习,所以什么是免费的我在学习,但它不起作用。谢谢!!!成功了。因为我不能在付费的地方学习,所以什么是免费的我在学习,但它不起作用。