如何使用 JavaScript 将图像右对齐

How to align image to right using JavaScript?

本文关键字:图像 右对齐 JavaScript 何使用      更新时间:2023-09-26

嗨,我正在尝试右对齐JS创建的"捐赠"按钮,如下面的代码所示,

<%@ Master Language="C#" AutoEventWireup="true" CodeBehind="MentorMentee.master.cs" Inherits="MentorMentee.MentorMentee" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
    <link href="style/style.css" rel="stylesheet" type="text/css" />
    <asp:ContentPlaceHolder ID="head" runat="server"> 
    </asp:ContentPlaceHolder>
</head>
<body>
    <form id="form1" runat="server" action="https://www.paypal.com/cgi-bin/webscr" method="post">
    <div id="main">
    <header>
      <div id="logo">
        <div id="logo_text">
          <!-- class="logo_colour", allows you to change the colour of the text -->
            <h1><span>M</span></h1>  
            <script type="text/javascript">
    function Donate()
{
    var myform = document.createElement("form");
    myform.action = "https://www.paypal.com/cgi-bin/webscr";
    myform.method = "post";
    myform.target = "_blank";
    var image = document.createElement("input");
    image.type = "image";
    image.src = "https://www.paypalobjects.com/en_US/i/btn/btn_donateCC_LG.gif";
    image.border = "0";
    image.name = "submit";
    image.alt="PayPal - The safer, easier way to pay online!";
    //Most probably this can be skipped, but I left it in here since it was present in the generated code
    var pixel = document.createElement("image");
    pixel.border = "0";
    pixel.alt="";
    pixel.src = "https://www.paypalobjects.com/en_US/i/scr/pixel.gif";
    pixel.width = "1";
    pixel.height = "1";

    myform.appendChild(image);

    myform.submit();
}
    </script>        
      </div>
      <nav>
        <asp:ContentPlaceHolder ID="HeaderContent" runat="server">
        </asp:ContentPlaceHolder>
        </nav>
        </div>
        </header>
        <div id="site_content">
        <div class="content">
                <asp:ContentPlaceHolder ID="MainContent" runat="server">
                </asp:ContentPlaceHolder>
            </div>
        </div>
    </div>
<img src="https://www.paypalobjects.com/en_US/i/btn/btn_donate_SM.gif"
border="0"
alt="PayPal - The safer, easier way to pay online!"
onclick="javascript:Donate();"
style="cursor:pointer;"/>
    </form>
</body>
</html>

但我愿意把它放在我这样做的同一行,

<h1><span>M</span></h1>
您可以将

ID 添加到图像标记并使用以下代码:

var myImageId = document.getElementById("IMAGE_ID");
document.getElementById("myButton").onclick = function () {myImageId.style.float = “left”;};

您可以设置任何属性,例如:填充,边距,...在左侧设置图像取决于您的页面