我已经创建了下面的功能,它搜索身体的一个特定的词,我能够得到结果,如果它存在或不存在。但是如果它存在的话,我如何找到这个特定的对象并与它相互作用呢?
(function ($) {
$.fn.WordBreaker = function (x) {
return this.each(function () {
var wrapper = $(this);
var xx = $.extend({
words: "",
}, x || {});
function initialized() {
xx.words.forEach(function (x, y) {
var lw, rw;
lw = x.toString().split(",")[0];
rw = x.toString().split(",")[1];
if ($("body:contains('" + lw + "')").length > 0) {
alert("I found an object that contains: " + lw + " , but how do i tager that object?")
}
}, xx.words)
}
initialized();
});
}
}(jQuery));
var items = [
["THISISALONGTEXTTHATIWANTTOBREAK", "THIS-IS-A-LONG-TEXT-THAT-I-WANT-TO-BREAK"]
];
$('.col-md-5').WordBreaker({ words: items })<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="row">
<div class="col-md-5" style="background: #00ffff">
<h1>THISISALONGTEXTTHATIWANTTOBREAK</h1>
</div>
</div>
发布于 2016-12-06 14:39:06
对于这个答案,我从Using jQuery is there a way to find the farthest (deepest, or most nested) child element?中提取了代码,并将其放在这里。最初的答案来自@Methos。
(function ($) {
$.fn.WordBreaker = function (x) {
return this.each(function () {
var wrapper = $(this);
var xx = $.extend({
words: "",
}, x || {});
function initialized() {
xx.words.forEach(function (x, y) {
var lw, rw;
lw = x.toString().split(",")[0];
rw = x.toString().split(",")[1];
var all_matched_elements = $(":contains('" + lw + "')");
var all_parent_elements = $(all_matched_elements).parents();
var all_deepest_matches = $(all_matched_elements).not(all_parent_elements);
console.log(all_deepest_matches); // this is an object containing the deepest objects that match the search string
}, xx.words)
}
initialized();
});
}
}(jQuery));
var items = [
["THISISALONGTEXTTHATIWANTTOBREAK", "THIS-IS-A-LONG-TEXT-THAT-I-WANT-TO-BREAK"]
];
$('.col-md-5').WordBreaker({ words: items })<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="row">
<div class="col-md-5" style="background: #00ffff">
<h1>THISISALONGTEXTTHATIWANTTOBREAK</h1>
</div>
</div>
https://stackoverflow.com/questions/40997444
复制相似问题