javascript - 如何在文本更改/添加时保持单选按钮静态

标签 javascript html css radio-button

我有一个单选按钮(列表),它会在单击“下一步”按钮时更改文本(使用 JavaScript)。文本会更改,但当文本变长时对齐会关闭,这使得它不会保持在恒定位置。

enter image description here

对齐后(向左移动了一些地方) enter image description here

Javascript(radio 是定义为“var”的变量): radio[0].nextSibling.textContent = "我们无法探索我们自己的知识";

HTML/CSS:

<div  style="position:absolute;transform: scale(2);top:80vh;left:20vw">
         <input type='radio' name='rblist1' value='0'> <br> 
         <input type='radio' name='rblist1' value='1'> <br> 
         <input type='radio' name='rblist1' value='2'>
           </div>

任何帮助都会很棒!谢谢

最佳答案

跳过当前的样式并在标签内构建您的 radio 。然后浏览器就会知道指定的文本属于 radio ,并且当单击该文本时 radio 也会激活。远离在具有多个元素的容器上使用 transform:scale 并且非常清楚 position: absolute 经常会咬你一口......

function changeText(){
  $("label>span").eq(1).text("This is an even longer text wich will test the structure of the HTML");
}
.transformScale2{
 transform: scale(2); 
}

.inputRadioContainer{
margin-bottom: 10px;
font-size: large;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div>
  <div class="inputRadioContainer">
    <label>
      <input type='radio' name='rblist1' value='0' class="transformScale2">
      <span>This is the label with a long text</span>
  </label>
  </div>
  <div class="inputRadioContainer">
    <label>  
      <input type='radio' name='rblist1' value='1' class="transformScale2">
      <span>This is the label</span>    
  </label>
  </div>
  <div class="inputRadioContainer">
    <label>  
      <input type='radio' name='rblist1' value='2' class="transformScale2">
      <span>This is the label</span>
  </label>
  </div>
 </div>
 
 <input type="button" onClick="changeText()" value="Click me"/>

关于javascript - 如何在文本更改/添加时保持单选按钮静态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52500632/

相关文章:

javascript - 如何从我的站点禁用(查看源代码)和(Ctrl + C)

javascript - 逗号分隔值(双引号内的值除外)

html - 烦人的右边距

css - recaptcha 2 宽度 css float 不起作用

javascript - 在 IE8 中无法使用 document.getElementById 找到 DOM

javascript - 如何使用 javascript 或 nodejs 更新具有公共(public)头文件的链接

html - 如何为每个 li 元素添加一个具有元素数量的类?

javascript - 尝试在 switch 语句中使用 $(window).resize(function() 时出现问题

html - Position Absolute 内部位置 绝对定位

javascript - 保存到数据库并重定向到新网址