我遇到了这个问题,我有一组链接,我需要获取所选链接的值。我正在使用 AJAX 来获取我想要的东西。但是还有别的办法吗?我当前的代码不适用于其他浏览器和 IOS 设备。不管怎样,事情是这样的:
例如我有 2 个动态生成的链接:
Link 1 (值(value)是谷歌)
Link 2 (值为雅虎)
假设我单击了链接 1。我需要获取“Google”链接的值(在我的例子中我使用了 title="Google"),并使用变量 $linkName 将该变量传递给 file_get_contents。所以它看起来像:
file_get_contents("http://domain.com/link_contents/includes/**$linkName**")
file_get_contents("http://domain.com/link_contents/includes/**Google**")
顺便说一句,我正在使用 CodeIgniter 框架。
截至目前,我有一个工作代码并使用了 AJAX。但是,它仅适用于使用以下设备的 Chrome 浏览器:桌面和 Android 设备。遗憾的是,它不适用于 IOS 设备。据我所知,几天前它还可以在 IOS 上使用,但突然间就不行了。我也在研究我的 AJAX 代码是否对 IOS 设备有影响。
无论如何。这实际上是我第一次使用 AJAX,所以很抱歉我的代码很乱。但我是这样做的:
在我的 header.php 上我有 ff:
$(document).ready(function(){ var getLink = $('.linkClass'); getLink.each(function(i){ $(this).click(function(){ var linkName = $(this).attr('title'); $.ajax({ url: 'http://domain.com/asset/file_get_contents.php', type: "POST", data: { getFromLinkClass: linkName } }).done(function( result ) { $('#divID').html(result); }); }); }); });
在我的 domain.com/asset/file_get_contents.php 文件上,
$linkName = $_POST["getFromLinkClass"]; if ($linkName != null) { $links = file_get_contents("http://domain.com/link_contents/includes/$linkName", true); echo $links;
}
最后,在我的 index.php 上,我可以调用 html 结果,它是
<div id="divID"></div>
正如我所说,这是有效的,但仅在 Chrome 中:在桌面和 Android Chrome 中。
有什么建议/想法/意见吗?或者是否有任何其他方法可以在其他浏览器和 IOS 设备上运行?
=======================
更新:
我更改了我的脚本,忽略了我的 AJAX 代码并改用纯 Javascript。它现在也适用于其他浏览器。
最佳答案
$links = file_get_contents("http://domain.com/link_contents/includes/".$linkName, true);
echo $links;
试试这个.. 如果你想访问外部 url,你必须添加 http://
或 https://
作为协议(protocol)。在 URL 之后连接 $linkName
关于javascript - 将动态生成的 href 标签的值传递给 file_get_contents,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22140791/