-1

我有以下代码需要添加到我正在 Wordpress 中处理的模板文件中。我用 php wp_enqueue_script("jquery"); 调用 jquery 这是版本 1.8.3。我已经在 header.php 文件中添加了脚本,虽然我可以使用 Dreamweaver 在本地运行它,但当我尝试在 wordpress 中运行时我不知道出了什么问题。

这是我正在使用的代码:

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<title> - jsFiddle demo</title>
<script type='text/javascript' src='http://code.jquery.com/jquery-1.8.3.js'></script>
<script type='text/javascript'>
$(window).load(function(){
function sortUsingNestedText(parent, childSelector, keySelector) {
var items = parent.children(childSelector).sort(function (a, b) {
    var vA = $(keySelector, a).text();
    var vB = $(keySelector, b).text();
    return (vA < vB) ? -1 : (vA > vB) ? 1 : 0;
});
parent.append(items);
}

/* setup sort attributes */
$('#sLocation').data("sortKey", "span.wpcf-field-location-value");
$('#sSchool').data("sortKey", "span.wpcf-field-schools-value");


/* sort on button click */
$("button.btnSort").click(function () {
sortUsingNestedText($('#sortThis'), "div", $(this).data("sortKey"));
});
});
</script>
</head>
<body>
<div id="sortThis">
<div class="fp-floorplans">
<div class="fp-link">
<a href="#">Community 1</a>
</div>
<div id="wpcf-field-location" class="wpcf-field-checkboxes wpcf-field-location"><span class="wpcf-field-name wpcf-field-checkboxes wpcf-field-location-name">Loation:</span>  <span class="wpcf-field-value wpcf-field-checkboxes-value wpcf-field-location-value">South</span>
</div>
<div id="wpcf-field-schools" class="wpcf-field-textfield wpcf-field-schools"><span class="wpcf-field-name wpcf-field-textfield wpcf-field-schools-name">School(s):</span>  <span class="wpcf-field-value wpcf-field-textfield-value wpcf-field-schools-value">Southwest</span>
</div>
<div id="wpcf-field-price-starting-at" class="wpcf-field-textfield wpcf-field-price-starting-at"><span class="wpcf-field-name wpcf-field-textfield wpcf-field-price-starting-at-name">Price Starting At:</span>  <span class="wpcf-field-value wpcf-field-textfield-value wpcf-field-price-starting-at-value">$100's</span>
    </div>
</div>
<div class="fp-floorplans">
    <div class="fp-link">
<a href="#">Community 3</a>
    </div>
    <div id="wpcf-field-location" class="wpcf-field-checkboxes wpcf-field-location"><span class="wpcf-field-name wpcf-field-checkboxes wpcf-field-location-name">Loation:</span>  <span class="wpcf-field-value wpcf-field-checkboxes-value wpcf-field-location-value">North</span>
    </div>
    <div id="wpcf-field-schools" class="wpcf-field-textfield wpcf-field-schools"><span class="wpcf-field-name wpcf-field-textfield wpcf-field-schools-name">School(s):</span>  <span class="wpcf-field-value wpcf-field-textfield-value wpcf-field-schools-value">Crowder</span>
    </div>
    <div id="wpcf-field-price-starting-at" class="wpcf-field-textfield wpcf-field-price-starting-at"><span class="wpcf-field-name wpcf-field-textfield wpcf-field-price-starting-at-name">Price Starting At:</span>  <span class="wpcf-field-value wpcf-field-textfield-value wpcf-field-price-starting-at-value">$100's</span>
    </div>
</div>
<div class="fp-floorplans">
    <div class="fp-link">
<a href="#">Community 2</a>
    </div>
    <div id="wpcf-field-location" class="wpcf-field-checkboxes wpcf-field-location"><span class="wpcf-field-name wpcf-field-checkboxes wpcf-field-location-name">Loation:</span>  <span class="wpcf-field-value wpcf-field-checkboxes-value wpcf-field-location-value">East</span>
    </div>
    <div id="wpcf-field-schools" class="wpcf-field-textfield wpcf-field-schools"><span class="wpcf-field-name wpcf-field-textfield wpcf-field-schools-name">School(s):</span>  <span class="wpcf-field-value wpcf-field-textfield-value wpcf-field-schools-value">Northeast</span>
    </div>
    <div id="wpcf-field-price-starting-at" class="wpcf-field-textfield wpcf-field-price-starting-at"><span class="wpcf-field-name wpcf-field-textfield wpcf-field-price-starting-at-name">Price Starting At:</span>  <span class="wpcf-field-value wpcf-field-textfield-value wpcf-field-price-starting-at-value">$100's</span>
    </div>
</div>
</div>
<button id="sLocation" class="btnSort">Sort By Location</button>
<br />
<button id="sSchool" class="btnSort">Sort By School</button>
<br />
</body>
</html>

非常感谢任何想法或帮助!

4

1 回答 1

0

只是在黑暗中刺伤,但包装所有使用的$代码

(function($){

    // your code here...

}(jQuery));

我认为 WordPress 默认情况下在 noConfilct 模式下有 jQuery 或类似的东西。

于 2013-04-08T01:11:05.187 回答