简体   繁体   English

如何在javascript的object属性值中包含php文件

[英]How to include php file in javascript's object property value

I'm trying to modify an object's value & as my knowledge on JS & JQuery is pretty limited, I'm troubled on how to make it work. 我正在尝试修改对象的值,并且由于我对JS和JQuery的了解非常有限,因此我对如何使其工作感到困扰。

Here is my code: 这是我的代码:

 var ProductData = { "Product1":"<li>some html</li>" "Product2":"<li>some more html</li>" } function loadData(ev, itemName) { ev.preventDefault(); closeMenu(); gridWrapper.innerHTML = ''; classie.add(gridWrapper, 'content-load'); setTimeout(function() { classie.remove(gridWrapper, 'content-load'); gridWrapper.innerHTML = '<ul class="products">' + dummyData[itemName] + '<ul>'; }, 700); } 

What I need is to load a php file in Product1, instead of html code. 我需要在Product1中加载一个php文件,而不是html代码。

I've managed to do it using an iframe , like below: 我设法使用iframe做到了,如下所示:

 var ProductData = { "Product1":"<iframe src=\\"dir/subdir/myFile.php\\"></iframe>" "Product2":"<li>some more html</li>" } 

but I'd prefer something like php's include function if there is something similar in JS! 但是如果JS中有类似的东西,我更喜欢php的include函数!

Any help would be highly appreciated! 任何帮助将不胜感激! Thanks in advance! 提前致谢!

For what your describing you would need to do an ajax request to retrieve the value. 对于您的描述,您将需要执行ajax请求以获取值。 Something like. 就像是。

var ProductData = {
    Product2: '<li>some more html</li>'
}

$.get("dir/subdir/myFile.php", function(data){
    ProductData.Product1 = data;
});

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

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