简体   繁体   中英

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:

     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? What is best source to learn more about such DOM binding with JS and JQuery

<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:

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

JQuery Script code:

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

otherwise use $("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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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