Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/83.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript&;JQuery:如何将值绑定到DOM元素_Javascript_Jquery_Html_Dom - Fatal编程技术网

Javascript&;JQuery:如何将值绑定到DOM元素

Javascript&;JQuery:如何将值绑定到DOM元素,javascript,jquery,html,dom,Javascript,Jquery,Html,Dom,以下是我网站的一部分 <body controller="contr"> <h1 bind="greeting"></h1> <p bind="text"></p> </body> 在以下情况下切换到h1和p标签: if controller == 'contr' 因此,它将被解释为 <h1 bind="greeting">Hello, Worl

以下是我网站的一部分

   <body controller="contr">
       <h1 bind="greeting"></h1>
        <p bind="text"></p>
   </body>
在以下情况下切换到h1和p标签:

     if controller == 'contr' 
因此,它将被解释为

     <h1 bind="greeting">Hello, World!</h1>
     <p bind="text">Lorem Ipsum ..</p>
你好,世界!
Lorem Ipsum.


如何在JQuery中实现这一点?要了解更多关于JS和JQuery的DOM绑定的信息,最好的来源是什么

$(“按钮”)。单击(函数(){
$('#greeting').html('Hello,World!');
$('#text').html('Lorem Ipsum..');
});


绑定
如果没有必要,请使用绑定属性

定义您的Html代码:

 <body id="contr">
   <h1 class="greeting"></h1>
    <p class="text"></p> </body>
否则使用$(“xxx”).attr(“绑定”、“值”)

贾斯珀 $(文档).ready(函数(){ $($h1[bind='greeting'].html('Hello World'); });
以前从未见过bind=”“像这样。为什么不使用数据绑定=“或类类型?是否使用Angular JS?@Grimbode这是主要问题,我不知道这里使用的是什么“控制器”和“绑定”属性。没有框架。这就是为什么我对如何进行dom绑定感到困惑的原因,而这段代码可能回答了这个问题,提供了关于为什么和/或这段代码如何回答这个问题的额外上下文,从而提高了它的长期价值。
 <body id="contr">
   <h1 class="greeting"></h1>
    <p class="text"></p> </body>
var greeting=  $("#contr").find(".greeting");
var text= $("#contr").find(".text");
greeting.html("Hello World!");
text.html("Lorem Ipsum ..");
<h1 bind="greeting">Jasper</h1>

<script language="javascript">
   $(document).ready(function () {
      $("h1[bind='greeting']").html('Hello World');
   });
</script>