添加元素动态后的动态window.location

时间:2012-05-11 10:17:02

标签: javascript html

我在页面上放了一个A标签,当用户点击它时,该页面将有一个新的输入字段来写入信息,如下所示:

HTML代码

<a href="" class="smallLink" onclick="return ioAddNewContent()">new info</a>

我不想在点击“新信息”后将哈希添加到网址,所以我让javascript返回null,如下所示:

javascript代码

    function ioAddNewContent(){
    var bigLi = document.getElementById('ioAddContents');
    var p = document.createElement('p');
    var label=document.createElement('label');
    var input = document.createElement('input');
    var a = document.createElement('a');
    label.setAttribute('class', 'ioAddOneContent');
    label.innerHTML='write one info';
    input.setAttribute('type', 'text');
    input.setAttribute('class', 'longInput');
    a.setAttribute('href', '');
    a.setAttribute('class', 'smallLink');
    a.setAttribute('onclick', 'return ioAddNewContent()');
    a.innerHTML=' new info';
    p.appendChild(label);
    p.appendChild(input);
    p.appendChild(a);
    bigLi.appendChild(p);
    return false;
}

点击“新信息”10次,用户必须向下滚动才能看到新输入字段,我不希望这样,我想要每次点击“新信息“屏幕应该转到新的输入字段

我知道我可以通过在html代码上添加名称=“here”的标签然后在javascript代码上放置`window.location(“#here”)`但我想做那个动态< / p>

当用户点击添加新输入字段时,屏幕应自动滚动到该新输入字段

2 个答案:

答案 0 :(得分:1)

您可以更改滚动容器上的scrollTop属性(例如正文或您的div)。

希望它有所帮助。

答案 1 :(得分:1)

使用.focus().last()

$('a.smallLink').last().focus();

这是jQuery集的一部分,不确定您是否使用了库。

相关问题