简体   繁体   English

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

[英]Javascript & JQuery: how to bind the value to DOM element

Given below is part of my website 以下是我网站的一部分

   <body controller="contr">
       <h1 bind="greeting"></h1>
        <p bind="text"></p>
   </body>

I need to bind the values of contr properties 我需要绑定控制属性的值

   contr.greeting = 'Hello, World!';
   contr.text = 'Lorem Ipsum ..';

to h1 and p tags when: 到h1和p标签时:

     if controller == 'contr' 

so it will be interpreted as 所以它将被解释为

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

How I can implement this in JQuery? 我如何在JQuery中实现它? What is best source to learn more about such DOM binding with JS and JQuery 使用JS和JQuery了解有关此类DOM绑定的更多信息的最佳来源是什么

<h1 bind="greeting">Jasper</h1>

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

if it is not necessary, to use the bind attribute... 如果没有必要,使用绑定属性...

Define your Html code: 定义您的Html代码:

 <body id="contr">
   <h1 class="greeting"></h1>
    <p class="text"></p> </body>

JQuery Script code: JQuery脚本代码:

var greeting=  $("#contr").find(".greeting");
var text= $("#contr").find(".text");
greeting.html("Hello World!");
text.html("Lorem Ipsum ..");

otherwise use $("xxx").attr("bind","value"); 否则使用$(“xxx”)。attr(“bind”,“value”);

You need to do something like this: 你需要做这样的事情:

 $('button').click(function() { $('#greeting').html('Hello, World!'); $('#text').html('Lorem Ipsum ..'); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <h1 id="greeting"></h1> <p id="text"></p> <button>Bind</button> 

If you want to use bindings and controllers you need to use mvw framework like AngularJS . 如果你想使用绑定和控制器,你需要使用像AngularJS这样的mvw框架。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM