简体   繁体   English

如何将这个html转换成jquery?

[英]How do i convert this html into jquery?

What I want to do is simply, adding all of these html elements by using jquery. 我想做的就是简单地通过使用jquery添加所有这些html元素。

Here's the code for the html, I'm using ejs as the templating engine 这是html的代码,我使用ejs作为模板引擎

example.ejs example.ejs

<div class="row" id="searchTest">
      <div class="col-md-3" id="searchColumn">
        <a href="/product/<%= products[i]._id %>">
          <div class="thumbnail">
            <img src="<%= products[i].image %>" alt="...">
            <div class="caption">
              <h3><%= products[i].name %></h3>
              <p><%= products[i].description %></p>
              <p>$ <%= products[i].price %></p>
              <p><%= products[i].category.name %></p>
            </div>
          </div>
        </a>
      </div>
    </div>

Above code is, Im using ejs to render the data from the server, and Im using AJAX call to append it to the row class 上面的代码是,Im使用ejs从服务器渲染数据,Im使用AJAX调用将其追加到row class

How do I convert the above code to jquery?, assume that the data has been received and the name is data 我如何将上面的代码转换为jquery ?,假设已接收到数据且名称为data

example.js example.js

$('#searchTest').append('<div></div>').addClass('col-md-3')
            .append('<a href=' + data._source._id + '</a>')
            .append('img src=' + data._source.image + '')

I only tried halfway because I'm really confused on how to do it. 我只尝试了一半,因为我对如何做感到很困惑。

Declare and append. 声明并附加。

var html = "";
html += '<div class="col-md-3">';
html += '<a href="' + data._source._id + '"></a>';
html += '<img src="' + data._source.image + '">';
html += '</div>';
$('.anyelement').append(html);

If you want a clear way to see the html structure, you could use '+' to join the html strings. 如果您想以一种清晰的方式查看html结构,可以使用'+'来加入html字符串。

var html = '<div class="col-md-3" id="searchColumn">' +
               '<a href="/product/' + data._source._id+ '">' +
                   '<div class="thumbnail">' + 
                       '<img src="' +data._source.image + '" alt="...">' +
                       '<div class="caption">' +
                           '<h3>' +data._source.name+ '</h3>' +
                           '<p>' + data._source.price + '</p>' +
                           '<p>' + data._source.category.name + '</p>' +
                       '</div>' +
                   '</div>' +
               '</a>' +
           '</div>'
;
$('#searchTest').append(html);

If you want more controlable of single nodes, you can make them piece by piece: 如果您想对单个节点进行更多的控制,可以将它们逐段设置:

$caption = $('<div class="caption">');
$caption.append($('<h3>').text(data._source.name))
        .append($('<p>').text(data._source.price))
        .append($('<p>').text(data._source.category.name));
$img = $('<img>');
$link = $('<a>');

$link.append($img).append($caption);
....
$wrapper = $('<div class="col-md-3" id="searchColumn">');
$wrapper.append($link);

$('#searchTest').append($wrapper);

Or, if you don't want any html tag in your javascript, you can also have a template in you html 或者,如果您不想在javascript中添加任何html标签,也可以在html中添加一个模板

<div class="col-md-3" class="searchColumnPrototype" style="display:none;">
    <a>
      <div class="thumbnail">
        <img />
        <div class="caption">
          <h3></h3>
          <p></p>
          <p></p>
          <p></p>
        </div>
      </div>
    </a>
  </div>

Then in javascript 然后在JavaScript

var $html = $('.searchColumnPrototype').clone();
$html.find('img').attr('src', data._source.image);
....
$html.show();
$('#searchTest').append($html);

when you try to append html you can use + something like this 当您尝试附加html时,可以使用+这样的东西

$('#searchTest').append('<div class="col-md-3">'+
                            '<a href="' + data._source._id + '"></a>'+
                            '<img src="' + data._source.image + '">'+
                        '</div>';
                        );

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

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