私がしたいのは、私のボタンサイズがです。私のdiv width
のです。私は動的に要素をdivに追加し、JavaScriptでは属性を追加します。divの幅に応じてボタンのサイズを変更する方法
var div2= document.createElement('div');
div2.className = "div2" ;
div2.id = "div2";
div2.style.color= ButtonColor;
div2.style.height = ButtonHeight;
div2.style.width = ButtonWidth;
div2.style.backgroundColor = BackgroundColor;
が、私は要素を作成し、このように追加します:だからここに.jsファイルの私のdivがある
div2.appendChild(h2) + "\n";
div2.appendChild(linebreak);
div2.appendChild(pic) + "\n";
div2.appendChild(linebreak);
var parentDiv = document.getElementById("surveybot-button");
var sp2 = document.getElementById("surveybot-link");
parentDiv.insertBefore(div2,sp2);
div2.appendChild(linebreak);
div2.appendChild(sp2);
は、その後、私は私のindex.phpに次に何
<div id="surveybot-button">
<a id="surveybot-link" class="button-1" href="https://gosurveybot.com/liberty-moving-video-chat-estimate/">SCHEDULE VIRTUAL ESTIMATE USIGN SURVEYBOT</a><br>
</div>
<script id="buttons-script" src="button.js" button-variant="<img src='img/button-icons-2.png'>" button-color="green" button-width="600px" button-height="355px" background-color="#11ff11">
</script>
<script>
var divWidth = document.getElementById("div2").offsetWidth + "px";
var divHeight = document.getElementById("div2").offsetHeight + 'px';
alert(divWidth);
alert(divHeight);
document.getElementsByClassName("button-1").style.width = divWidth/3.2 + "%";
//document.getElementsByClassName("button-1").style.width = divWidth - 100px;
これは私が試したものです:
document.getElementsByClassName("button-1").style.width = divWidth/3.2 + "%";<br>
document.getElementsByClassName("button-1").style.width = divWidth * 0.3;
最後にCSS:
a.button-1 {
width: 300px;
height: 100px;
background: url(img/button-button-1.png) top center no-repeat;
background-size: 100% auto;
text-indent: -999999px;
color: transparent;
float: left;
cursor: pointer;
position: absolute;
}
誰かが私に間違っていることを教えてもらえますか?ですから、私の目標を繰り返すには、divが100pxボタンが35px(div幅の35%)とpicture90px(div幅の90%)でなければなりません。
すべてのアドバイスと解決策は大歓迎です。 ありがとうございます。
多分私は間違った質問を理解しているかもしれませんが、あなたは単にCSSの幅を35%に設定できませんか? – Fels