js脚本文件中的WordPress路径URL [英] WordPress path url in js script file

查看:135
本文介绍了js脚本文件中的WordPress路径URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我添加了自定义脚本:

wp_enqueue_script('functions', get_bloginfo('template_url') . '/js/functions.js', 'search', null, false);

它的效果很好,除了 functions.js 我有:

It works great, except in the functions.js I have:

Reset.style.background = "url('../images/searchfield_clear.png') no-repeat top left";

以前用过,直到我改为非常永久链接和.htaccess

This used to work before, until I changed to pretty permalinks and .htaccess

文件夹层次结构如下:


themename / js themename / images (图片和js文件夹位于theameame文件夹中)

themename/js themename/images (the images and js folder are in themename folder)

我试过../images - ./image - / images

I tried ../images - ./image - /images

正常情况下,无论js文件位于何处,都应该返回1级....

Normally it should go back 1 level wherever the js file is located....

我不想使用完整路径。

WordPress是否有另一种方法可以在javascript文件中识别出正确的路径?

Is there another way that WordPress can recognize in the javascript file to have the correct path?

目前我很困惑我做错了什么。

Currently I am just confused what I am doing wrong.

推荐答案

你可以避免硬编码在调用 wp_head()之前,通过在模板的标题中设置JS变量来保存模板URL。喜欢:

You could avoid hardcoding the full path by setting a JS variable in the header of your template, before wp_head() is called, holding the template URL. Like:

<script type="text/javascript">
var templateUrl = '<?= get_bloginfo("template_url"); ?>';
</script>

并使用该变量设置背景(我知道你知道如何做到这一点,我只包括这些细节以防万一):

And use that variable to set the background (I realize you know how to do this, I only include these details in case they helps others):

Reset.style.background = " url('"+templateUrl+"/images/searchfield_clear.png') ";

这篇关于js脚本文件中的WordPress路径URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆