简体   繁体   English

JavaScript使用onclick()函数更改值

[英]JavaScript change value using onclick() function

I am trying to change the value of x, using the changePost function. 我正在尝试使用changePost函数更改x的值。

When the changePost function is fired it should change the value of x to the corresponding posts[i]. 触发changePost函数时,应将x的值更改为相应的posts [i]。

   var x = 0; var title = posts[x].title, date = posts[x].date, content = posts[x].content, author = posts[x].author;
      <article>
        <h2>$title</h2>
        <small>$date.format()</small>
        <p>$content</p>
        <cite>$author</cite>
      </article>

      for(var i in posts) { var title = posts[i].title, date = posts[i].date, content =   posts[i].content, author = posts[i].author;
      <article>
        <h3 onclick="changePost()">$title</h3>
        <small>$date.format()</small>
      </article>
      }
    </aside>

    function changePost(){ var x = i; };

Thanks. 谢谢。

Your problem is not just changing the x value, you have to re-render the template every time you change the x . 您的问题不只是更改x值,而且每次更改x时都必须重新渲染模板。

var main_template = document.getElementById('mainTemplate').innerHTML;
function changePost(x) {
    main.post_id = x;
    document.getElementById('main').innerHTML = T(main_template, main);
}

Here's the fiddle: http://jsfiddle.net/bcq7580s/5/ 这是小提琴: http : //jsfiddle.net/bcq7580s/5/

Alternative would be to render all the post, hide them all via css, and then when the user clicks the link, hide all visible posts, and then show just the one the user picked. 另一种方法是呈现所有帖子,通过css全部隐藏它们,然后当用户单击链接时,隐藏所有可见的帖子,然后仅显示用户选择的帖子。

I understand you want to browse an array of posts by clicking a next button. 我了解您想通过单击下一步按钮来浏览一系列帖子。 I would advise you to separate your post template, your post-browse functionality and the events that must trigger the change of post content. 我建议您将帖子模板,浏览后功能和必须触发帖子内容更改的事件分开。

For creating a template you could use Underscore's template function . 要创建模板,可以使用Underscore的template函数 Or EJS . EJS And probably many other template libraries. 可能还有许多其他模板库。

For manipulating the dom I would advise jQuery but you could do without. 对于操纵dom,我建议jQuery,但您可以不这样做。

Make sure all HTML is loaded before you try access elements in the page. 在尝试访问页面中的元素之前,请确保已加载所有HTML。

Visit this JS Fiddle 访问此JS小提琴

The markup: 标记:

<div id="post"></div>
<a href="#" id="next">next</a>

The code: 编码:

// A simple wrapper for your posts so your index variable cant be overwritten 
// by some other peace of code
var posts = (function() {

    // The template for the posts, using Underscore's template function
    var tpl = _.template("<h2><%= title %></h2><p><%= content %></p>");

    // The index of the current post
    var index = 0;

    // The array of posts
    var posts = [
        {title:"Title 1", content:'Bla bla lorem'},
        {title:"Title 2", content:'Bla bla ipsum'},
        {title:"Title 3", content:'Bla bla dolor'},
        {title:"Title 4", content:'Bla bla sit'},
        {title:"Title 5", content:'Bla bla amet'}
    ];

    // The functions available to the document
    return {
        next:function(){
            if(++index >= posts.length) {
                index = 0;
            }
            return tpl(posts[index]);
        },
        load:function(){
            return tpl(posts[index]);
        }
    }
}());

// Use of your posts object by the html document
document.getElementById('post').innerHTML = posts.load();
document.getElementById('next').onclick = function(){
    document.getElementById('post').innerHTML = posts.next();
}

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

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