Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/69.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 在同一个div中打开包含在div中的动态链接(如更改/更新博客页面)_Javascript_Html - Fatal编程技术网

Javascript 在同一个div中打开包含在div中的动态链接(如更改/更新博客页面)

Javascript 在同一个div中打开包含在div中的动态链接(如更改/更新博客页面),javascript,html,Javascript,Html,我现在有下面的脚本,它抓取div(scrollbox)中的链接,正如您所看到的,它被设置为在一个新窗口中打开链接。我需要在div中打开它们,它们来自原始页面。我可以用这个脚本做这个吗?如果没有,我应该使用什么 function Init() { // Grab the appropriate div theDiv = document.getElementById('scrollbox'); // Grab all of the links inside the div

我现在有下面的脚本,它抓取div(scrollbox)中的链接,正如您所看到的,它被设置为在一个新窗口中打开链接。我需要在div中打开它们,它们来自原始页面。我可以用这个脚本做这个吗?如果没有,我应该使用什么

function Init()
{
    // Grab the appropriate div
    theDiv = document.getElementById('scrollbox');
    // Grab all of the links inside the div
    links = theDiv.getElementsByTagName('a');
    // Loop through those links and attach the target attribute
    for (var i=0, len=links.length; i < len; i++) {
        // the _blank will make the link open in new window
        links[i].setAttribute('target','_blank');
    }
}
window.onload = Init;
函数Init()
{
//抓取合适的div
theDiv=document.getElementById('scrollbox');
//抓取div中的所有链接
links=theDiv.getElementsByTagName('a');
//循环浏览这些链接并附加目标属性
对于(变量i=0,len=links.length;i

非常感谢你的帮助 <代码>标签。 但也许我不完全理解您需要什么

查看JQuery的函数

像这样的方法应该会奏效:

<div id="webpageContainer"></div>

$( "#link" ).click( function()
{
    $( "#webpageContainer" ).load( $(this).attr( 'href' ));
});

$(“#链接”)。单击(函数()
{
$(“#webpageContainer”).load($(this.attr('href'));
});

您的意思是DIV内容应该根据网页的内容进行更改吗?如果是这样的话,AJAX可能是合适的。@pimvdb有点像,但不是真的。DIV的内容将定期更新,但内容只应在单击链接后更改。因此,我不能只输入链接并将其设置为静态目标。我必须使用上面的脚本“抓取”DIV中的任何链接,并设置这些链接的目标,无论它们当时是什么。感谢您的解释。但是如果我没有错的话,目标不是一个窗口而是一个DIV元素?因为在这种情况下,您需要一些AJAX技术,以便在不刷新当前页面的情况下加载数据(或者使用
iframe
)。是的,您的思路是正确的,但由于兼容性问题,我希望避免使用。