从输入值更改css属性

时间:2017-03-19 00:30:44

标签: jquery css

我现在正在上课,我和我的小组正在尝试创建一个允许孩子使用输入值操纵CSS的游戏。出于某种原因,每次我尝试操作DOM时都会出现错误。

错误:

未捕获的TypeError:无法设置属性' html'未定义的     在HTMLButtonElement。 (?challenges.coffee.self-f525f98 ...的.js体= 1:18)     在HTMLButtonElement.dispatch(jquery.self-bd7ddd3 ... .js?body = 1:5227)     在HTMLButtonElement.elemData.handle(jquery.self-bd7ddd3 ... .js?body = 1:4879)

这是jQuery:

$(".code_generator").click(function() {
    answer = $("#answer").val()
    console.log(answer)
    $(".profile-picture").css("text-align", answer)
  })

HTML:

    <p class="picture_positioning">img { <br> text align:</p>
        <input type="text" name="profile_pic" id="answer" />
        <button class = "code_generator" type="button">Submit code!
        </button>

1 个答案:

答案 0 :(得分:0)

你可以这样做:

<input type="text" name="profile_pic" id="answer" />
<button type="button" onclick="submit();">Submit code!</button>

js提交功能

function submit(){
  var my_input = document.getElementById("answer").value();

  var obj = document.getElementsByClassName("profile-picture")[0];
  obj.style.setProperty(my_input);
}