使用Javascript将按钮添加到HTML页面 [英] Add button to an HTML page using Javascript

查看:81
本文介绍了使用Javascript将按钮添加到HTML页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试修改页面的HTML标记,以便使用JavaScript添加一些按钮。

I'm trying to modify the HTML markup of a page in order to add some button using JavaScript.

下面你可以找到一个代码段(很长的,我很抱歉,但我真的需要你获得页面的结构)我想修改的页面。

Below you can find a "snippet" (quite long, I apologize, but I really need you to get the structure of the page) of the page I am trying to modify.

我的JavaScript代码由浏览器扩展插入(我可以成功地将JavaScript添加到页面并使其运行),唯一的操作是将按钮添加到正确的位置。

My JavaScript code is inserted by a browser extension (I can successfully add the JavaScript to the page and make it run) and the only operation it should do is to add a button into the right position.

HTML页面包含一个< FORM> ,其中包含一个表。

在一些行和单元格后面有一个包含3个输入按钮的单元格:

The HTML page contains a <FORM> with a table in it.
After some rows and cells there is a cell which contains 3 input buttons:

<input type="submit" name="submit" value="Set Ships">
<input type="button" name="sel" value="Select all" onclick="alert('Not Allowed.');">
<input type="button" name="desel" value="Deselect all" onclick="alert('Not Alloowed.');">

我希望我的JavaScript代码在 desel之后放置第四个按钮按钮。
这是我用来添加按钮的代码:

I would like my JavaScript code to place a fourth button just after the desel button. This is the code I use to add the button:

function add() {
    var element = document.createElement("input");
    element.setAttribute("type", "button");
    element.setAttribute("value", "invert");
    element.setAttribute("name", "button3");
    element.setAttribute("onclick", "foo()");
    document.flotta.appendChild(element);
}

此代码显然将按钮直接放在文档的末尾,表单(名为 flotta )。

This code obviously places the button straight at the end of my document, just after the form (named flotta).

我意识到我不能使用函数 getElementById 因为< td> 标签之前的按钮没有一个 id 关联。

I realized I cannot use the function getElementById because the <td> tag just before the buttons does not have an id associated.

所以我问是否有人可以指出一个解决方案,将第四个按钮添加到正确的地方。

Thus I ask if anyone can point me to a solution to add the fourth button into the right place.

<html>
    <head>
    <title>fee foo</title>  
        . . . head code
    </head>

<body >
    <div id="Layer" name="Layer" /*other attributes*/"></div>
    <table /*table attributes*/>
    . . . table content
    </table>
<form id="flotta" name="flotta" method="post" action="home.php?sel=gestioneflotta">
    <table /*table attributes*/>
        <tbody>
            <tr><td>
            <table /* attributes*/>
                <tbody><tr>
                <td /* attributes*/></td>
                <td /* attributes*/></td>
                <td /* attributes*/></td>
                </tr>
                <tr>
                <td /* attributes*/">&nbsp;</td>
                <td bgcolor="ffffff" background="bg/pergamena.jpg" align="center">
                <input type="submit" name="submit" value="Set Ships">
                <input type="button" name="sel" value="Select all" onclick="alert('Not Allowed.');">
                <input type="button" name="desel" value="Deselect all" onclick="alert('Not Alloowed.');">   </td>
                <td background="bg/menu_d.gif">&nbsp;</td>
                </tr>
                <tr>
                <td /* attributes*/" width="11" height="11"></td>
                <td /* attributes*/></td>
                <td /* attributes*/></td>
            </tr>
            </tbody>
            </table>
        </td></tr>
            <tr>
            . . . another row
            </tr>

        </tbody>
    </table>
</form>
<table border="0" cellspacing="0" cellpadding="0" align=center  width=510>
    . . . another table
</table>

<script type="text/javascript">
some script
</script>
</body>

</html>


推荐答案

以下代码应该得到td:

The following code should get the td:

var td = document.getElementsByName('desel')[0].parentNode;

Basi cally,它得到所有的字段/按钮,名称为'desel',假设只有一个,获取第一个元素(应该是包含按钮的td)的父项。

Basically, it gets all the fields/buttons with the name 'desel' and, assuming there's only one, gets the parent of the first element (which should be the td that contains the buttons).

这篇关于使用Javascript将按钮添加到HTML页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆