Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/sharepoint/4.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
Jquery 用JSON调用中的内容替换div_Jquery_Json_Html - Fatal编程技术网

Jquery 用JSON调用中的内容替换div

Jquery 用JSON调用中的内容替换div,jquery,json,html,Jquery,Json,Html,我正在接收一个JSON调用的内容,我想在news\u item类中填充3个返回响应的对象,如下所示: <div class="news"> <div class="news_item" style="margin-left: 0 !important;"> <div class="ni_image"> <a href="/HaberDetay/TITLE/ID"><img src="

我正在接收一个JSON调用的内容,我想在
news\u item
类中填充3个返回响应的对象,如下所示:

<div class="news"> 
        <div class="news_item" style="margin-left: 0 !important;">

        <div class="ni_image">
            <a href="/HaberDetay/TITLE/ID"><img src="IMAGE"width="203" height="109"/></a>
        </div>
        <div class="ni_text">
            <div class="ni_text_title">TITLE</div>
            <div class="ni_text_content"> 
                CONTENT
            </div>
        </div>
        </div> 
</div>
有什么建议吗?我该怎么做


编辑:我正在恢复标题、ID、内容等。我只需要看看如何创建这个新的div。

请注意,jQuery模板仍处于测试阶段(可以在此处使用DL:)

创建此HTML的jQuery模板,标题为/ID/Content等,它们将出现在
${Title}
/等处。基本上如下所示:

<div class="news_item" style="margin-left: 0 !important;">
    <div class="ni_image">
        <a href="/HaberDetay/${TITLE}/${ID}"><img src="IMAGE"width="203" height="109"/></a>
    </div>
    <div class="ni_text">
        <div class="ni_text_title">${TITLE}</div>
        <div class="ni_text_content"> 
            ${CONTENT}
        </div>
    </div>
</div> 

${TITLE}
${CONTENT}
然后创建模板并将其附加到新闻中

$('#yourTemplateScriptArea').tmpl(newsByCity).appendTo('.news')

如果你以前从未做过模板制作,这里有一个关于模板制作的介绍。

要创建新元素,可以使用JS或jQuery创建它们。jQuery更简单,但开销更高

// Element to append to
var target = $('.news');

// Create the new element in a jQuery object
// and use the appendTo method to attach it
// to the DOM element
$('<div />').html('<p>New div created</p>')
            .appendTo(target);
//要附加到的元素
var target=$('.news');
//在jQuery对象中创建新元素
//并使用appendTo方法附加它
//到DOM元素
$(“”).html(“新创建的div

”) .附于(目标);
或者,您可以找到现有的dom元素并替换文本或HTML

var target = $('.news');

// Replacing text
target.find('.ni_text_title').text('New news title');

// Replacing HTML
target.find('.ni_text').html('<div class="ni_news_text">...</div>');
var target=$('.news');
//替换文本
target.find('.ni_text_title').text('新新闻标题');
//替换HTML
target.find('.ni_text').html('..');
通过将空字符串传递给
.HTML
.text
方法,可以清空元素的文本或HTML内容。如果将它们留空,该方法将返回该元素的当前文本或HTML


希望能有帮助。

是的。我该怎么做?谢谢。我无法将城市、标题和国家与您描述中的标题、ID、内容以及div类“ni_text”、“ni_image”、“ni_text_Title”和“ni_text_CONTENT”进行匹配。JSON的哪一部分最终会出现在哪个DIV中?JSON的具体外观如何?@devnull69假设对象中有标题、ID等。jQuery模板看起来不错,但值得注意的是,它仍处于测试阶段,尚未准备好投入生产使用。是的,我忘了提到这一点!您还可以查看
knockout.js
。它处理模板的方式非常相似,而且本身就令人难以置信。我认为我不需要淘汰。我知道这一点。这真的不符合我的目的。谢谢
var target = $('.news');

// Replacing text
target.find('.ni_text_title').text('New news title');

// Replacing HTML
target.find('.ni_text').html('<div class="ni_news_text">...</div>');