【问题标题】:Embedding external jQuery and CSS file to a index.php file将外部 jQuery 和 CSS 文件嵌入到 index.php 文件中
【发布时间】:2015-03-29 09:44:37
【问题描述】:

我有一个具体的问题。 如何在 php 文件扩展名中嵌入外部 .js 或 .css 文件?

它不能像我将它嵌入到 html 文件扩展名中那样工作。

【问题讨论】:

  • ?
  • 这不是一个合适的问题,因为它可以有多种解释方式。请更具体地说明您的实际问题是什么并提供代码大纲

标签: javascript php jquery html css


【解决方案1】:

您通常会将这些包含在 the <head> 标签中

<link rel="stylesheet" type="text/css" href="myStyles.css">
<script src="myScript.js"></script>

但是如果你出于某种原因必须使用php,你可以使用

<? include "myScript.php"; ?>

编辑:

针对您关于定位输出文本的评论,您只需在可识别标签之间回显文本。例如:

<span id:="targetThis">
    <? echo "Text" ?>
</span>

现在您可以使用以下命令在 Css 中定位文本:

#targetThis {
    color: red;
}

您也可以使用 JQuery 获取它:

$('#targetThis') then do whatever it is you wanted to do with it...

例如

$('#targetThis').html('Edited Text');

【讨论】:

  • 但是如果我只有一个 php 文件,其中只有 php 代码.. 并且我有一些回声(文本输出显示在浏览器上)。如何向其中添加 css 和 jquery?
  • 我已编辑答案以显示针对该特定回声的定位
  • 为什么我不能对 php 使用与 html 扩展文件类似的技术?
  • 你可以直接回显整个跨度,比如&lt;? echo 'span class="targetThis"&gt;Text&lt;/span&gt;'; ?&gt; 我只是发现HTML和PHP格式的代码更容易阅读,只是个人喜好
猜你喜欢
  • 1970-01-01
  • 2018-12-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-06-14
  • 2018-04-25
  • 1970-01-01
  • 2011-06-07
相关资源
最近更新 更多