JavaScript addEventListener 在单击时添加文本框?

发布于 2024-12-19 05:14:45 字数 490 浏览 0 评论 0原文

我需要能够单击一些文本(id =“txt”)并触发一个事件,用文本框替换该文本;目前我有这段代码:

var myTxt = document.getElementById("txt");
myTxt.addEventListener("click", name1Click, false);

function textclick()
{
    var removeMe = document.getElementById("txt");
    var root = document.body;
    root.removeChild(removeMe);
    var addMe = document.createElement("input");
    addMe.setAttribute("type","text");
    addMe.setAttribute("id","i");
    root.appendChild(addMe);
}

这不起作用,但是有什么想法吗?

I need to be able to click on some text (id="txt") and fire an event which replaces that text with a textbox; currently I have this code:

var myTxt = document.getElementById("txt");
myTxt.addEventListener("click", name1Click, false);

function textclick()
{
    var removeMe = document.getElementById("txt");
    var root = document.body;
    root.removeChild(removeMe);
    var addMe = document.createElement("input");
    addMe.setAttribute("type","text");
    addMe.setAttribute("id","i");
    root.appendChild(addMe);
}

This is not working, however, any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

树深时见影 2024-12-26 05:14:45

在您在此处发布的代码中,您的 eventListener 正在调用 name1Click,但您的函数名为 textclick

当你解决这个问题时,它似乎在这里工作: http://jsfiddle.net/jfriend00/55mQq/

您可能需要注意,旧版本的 IE 不支持 addEventListener()(您必须使用 attachEvent() 代替)。

In the code you have posted here, your eventListener is calling name1Click, but your function is named textclick.

When you fix that, it seems to work here: http://jsfiddle.net/jfriend00/55mQq/.

You may want to note that addEventListener() is not supported in older versions of IE (you have to use attachEvent() instead).

夏尔 2024-12-26 05:14:45

在您的代码中:

> var myTxt = document.getElementById("txt");
> myTxt.addEventListener("click", name1Click, false);

您在 addEventListener 中添加一个名为 name1Click 的函数,但您的函数名为 textclick。修复这个问题,它就“有效”

另外:

> function textclick() {
>     var removeMe = document.getElementById("txt");
>     var root = document.body;
>     root.removeChild(removeMe);

removeMe 元素可能并不总是 body 的子元素,因此您可以更一般地删除它:

  removeMe.parentNode.removeChild(removeMe);

但是可以替换节点比删除,见下文。

>     var addMe = document.createElement("input");
>     addMe.setAttribute("type","text");
>     addMe.setAttribute("id","i");

setAttribute 方法在某些浏览器中存在错误,如果不需要,最好避免使用。对于标准 DOM 属性,只需直接设置属性:

    addMe.type = 'text';
    addMe.id = 'i';

>     root.appendChild(addMe); 

您可以只使用一个操作并一般替换第一个节点,而不是删除一个节点并插入另一个节点:

  removeMe.parentNode.replaceChild(addMe, removeMe);

> }

In your code:

> var myTxt = document.getElementById("txt");
> myTxt.addEventListener("click", name1Click, false);

You are adding a function named name1Click in addEventListener, but your function is called textclick. Fix that and it "works"

Also:

> function textclick() {
>     var removeMe = document.getElementById("txt");
>     var root = document.body;
>     root.removeChild(removeMe);

It is likely that the removeMe element won't always be a child of the body, so you can remove it more generically with:

  removeMe.parentNode.removeChild(removeMe);

But the node can be replaced rather than removed, see below.

>     var addMe = document.createElement("input");
>     addMe.setAttribute("type","text");
>     addMe.setAttribute("id","i");

The setAttribute method is buggy in some browsers and better avoided if not needed. For standard DOM properties, just set the property directly:

    addMe.type = 'text';
    addMe.id = 'i';

>     root.appendChild(addMe); 

Rather than removing one node and inserting another, you can just use one operation and replace the first one generically:

  removeMe.parentNode.replaceChild(addMe, removeMe);

> }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文