JQuery yordamida matn yorlig'i ichidagi matnning birinchi yarmini tanlang

Matnning birinchi yarmi jQuery orqali ulanadigan yorlig'i ichiga nishon olish uchun usul bormi?

Misol uchun, menda bu teglar bor.

Go to Meeting Center <!--/target 'Go to' -->
Our Departments <!--/target 'Our' -->

Men ularni span tagida o'ramoqchiman.

Natijada quyidagilar bo'lishi kerak:

Go to Meeting Center <!--/target 'Go to' -->
Our Departments <!--/target 'Our' -->

Bu mening veb-saytimdagi barcha ankrajlarimga qo'llaniladi.

1
@MikeMcCaughan Menimcha, OV harflar o'rniga so'zlarning sonini nazarda tutadi.
qo'shib qo'ydi muallif uom-pgregorio, manba
@LiberalTearsDrinker 5 so'z haqida nima deyish mumkin?
qo'shib qo'ydi muallif uom-pgregorio, manba
@LiberalTearsDrinker OK javobni tekshiring.
qo'shib qo'ydi muallif uom-pgregorio, manba
"Birinchi yarim" nima degani? Sizning namunalaringiz men biladigan har qanday o'lchov bilan "yarim" emas.
qo'shib qo'ydi muallif Mike McCaughan, manba
kutilgan natijaga qanday qarash kerakligini ko'rsating
qo'shib qo'ydi muallif RomanPerekhrest, manba
Ha, elementi ichida elementini oddiygina qo'yish mumkin: jQuery bilan uni tanlash uchun $ ("# firsthalf") ga qo'ng'iroq qiling. Uchrashuvga o'tish
qo'shib qo'ydi muallif mike510a, manba
Men savolni faqat yangiladim. Yarimga qaraganda, demirsimon tegining ichidagi so'zlarning soni ikkiga bo'linadi. Shunday qilib, agar sizda 4 ta so'z bo'lsa, u dastlabki ikki so'zni maqsad qilib qo'yishi kerak. Oltita so'zingiz bo'lsa, u dastlabki uchta so'zni maqsad qilib qo'yishi kerak.
qo'shib qo'ydi muallif Liberal Tears Drinker, manba
@ uom-pgjorio, yaxshi savol. O'ylaymanki, u faqat birinchi yoki ikkinchi va ikkinchi so'zlarni maqsad qilib qo'yishi kerak. Matnning yarmini nishonlashni istaganimning sababi shundaki, ularni boshqacha uslubda tarbiyalashim mumkin.
qo'shib qo'ydi muallif Liberal Tears Drinker, manba

9 javoblar

Buni qilishingiz mumkin:

var text = $("a").text().split(" ");
var half_of_text = text.splice(0, Math.floor(text.length/2)).join(" ");
2
qo'shib qo'ydi
rozi bo'ldim, faqat O'Zdan ishora qildim :)
qo'shib qo'ydi muallif Brad, manba
Men bu ishni faqat bitta odamga qaratgan deb hisoblayman. Istaganingizcha amalga oshirish juda oson. Bu masala uchun bir xil kontseptsiya
qo'shib qo'ydi muallif user3329290, manba

Quyidagi kod barcha teglar orqali ko'chiriladi, kerakli yarim matnni topadi (ushbu javobni yordamida) va so'ngra oraliqni bu yarmicha matnni yozib, spanslar uchun yaratgan spans diviga qo'yadi.

Buni JSFiddle tekshirib ko'ring.

HTML

Go to Meeting Center <!--/target 'Go to' -->
Our Departments <!--/target 'Our' -->
<div id="spans"></div>

JavaScript/JQuery

$('a').each(function(i, ele) {
    text = $(this).text().split(" ");
    half_of_text = text.splice(0, Math.floor(text.length/2)).join(" ");

    span = $(document.createElement('span'));
    span.html(half_of_text);
    $('#spans').append(span);
    $('#spans').append(document.createElement('br'));
});
1
qo'shib qo'ydi

Ushbu Fiddleni tekshiring

HTML

Go to Meeting Center <!--/target 'Go to' -->
Our Departments <!--/target 'Our' -->

jQuery

$(document).ready(function() {
  $.each($("a"), function(index, element) {
    var text = $(element).text().split(" ");
    text = text.splice(0, Math.floor(text.length/2)).join(" ");

    $(element).HTML($(element).text().replace(text, "" + text + ""))
  });
});

CSS

a {
  display: block;
}

span {
  color: red !important;
}

QAYD:

     

Agar qavat dan foydalanmoqchi bo'lsangiz, tavan dan foydalaning.

0
qo'shib qo'ydi
Bu juda yaxshi ishlaydi. Sizga katta rahmat!
qo'shib qo'ydi muallif Liberal Tears Drinker, manba

Quyidagi yondashuvdan foydalaning:

<div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false"> <div class="snippet-code">

$('a').each(function(i, el){
    var parts = $(el).text().split(' '); //splitting anchor text into chunks
    $(el).html(""+ parts.splice(0, Math.ceil(parts.length/2)).join(" ") +" " + parts.join(" "));
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
Go to Meeting Center <!--/target 'Go to' -->
Our Departments
</div> </div>

Used functions:
JQuery .each()
String.prototype.split() (to split anchor text into chunks)
Array.prototype.splice() (to extract the first half of the text)
Array.prototype.join()

0
qo'shib qo'ydi

Boshqa barcha javoblardagi kodlardan ayrimlarini sayt bo'ylab barcha havolalarga tatbiq qilish uchun qulay foydalanish usulini yaratishingiz mumkin

(parchani ko'rish)

<div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false"> <div class="snippet-code">

function get_first_half(element) {
  var text = $(element).text().split(" ");
  var half_of_text = text.splice(0, Math.floor(text.length/2)).join(" ");
  return half_of_text;
}

function get_back_half(element) {
  var text = $(element).text().split(" ");
  var half_of_text = text.slice(Math.floor(text.length/2), Math.floor(text.length)).join(" ");
  return half_of_text;
}

$(document).ready(function() {
  $("a").each(function(elem) {
    var first_half = get_first_half(this);
    var back_half = get_back_half(this);
    $(this).html("" + first_half + "" + back_half);
  });
});
span {
  font-size: 2.0em;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
Goto Meeting

Our Departments
</div> </div>
0
qo'shib qo'ydi

<div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false"> <div class="snippet-code">

$("a").each(function(){
  console.log($(this).text().substr( 0 ,$(this).text().length/2));
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
Go to Meeting Center
Our Departments
</div> </div>
0
qo'shib qo'ydi

Buni sinashingiz mumkin:

var t = $('a').text();
var middle= '';
for(var i =0; i

PS: Bu testdan o'tmadi va faqat tasvirlash uchun qilingan

0
qo'shib qo'ydi

Siz o'zingizning o'rtangizda qaerda ekanligingizni hal qilishingiz kerak, ammo bu erda so'zlarning soni bilan o'rtani aniqlashning yo'lidir. Siz ushbu echim bilan bir so'zni aylanasiz.

Dastlab havoladagi matnni olishingiz kerak va argumentga kiritilgan ajratgichga ko'ra, magistraldan bir qator hosil qiluvchi split funksiyasidan foydalaning.

So'ngra, siz o'zgartirgan so'zlarni o'z ichiga olgan span elementini yaratasiz va havoladagi matnni o'sha vaqt oralig'ida o'zgartirasiz.

<div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false"> <div class="snippet-code">

var links = document.querySelectorAll('a');
[].forEach.call(links, function(link_el){
  var text_inside = link_el.innerText;
  var words = text_inside.split(' ');
  var words_half = Math.ceil(words.length/2);
  var span_element = document.createElement('span');
  var words_string = words.splice(0, words_half).join(' ');
  link_el.innerText = link_el.innerText.replace(words_string, '');
  span_element.innerText = words_string;
  link_el.prepend(span_element);
});
Go to Meeting Center <!--/target 'Go to' -->
Our Departments <!--/target 'Our' -->
Contact <!--/target 'Contact' -->
</div> </div>
0
qo'shib qo'ydi
Bu juda yaxshi ishlaydi. Katta rahmat!
qo'shib qo'ydi muallif Liberal Tears Drinker, manba

"Birinchi yar" - juda sub'ektiv atamadir.

Buni o'zingiz kodlashingiz kerak, deb qo'rqaman,
juda muhim yondashuv:

<div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false"> <div class="snippet-code">

//assuming this is your query string
var text = "Go to Meeting Center";

//split string into an array
var terms = text.split(" ");

//looking for an approximate value the center of the text
var half = Math.ceil(terms.length/2);

//first "half" of query
var selected = terms.splice(0, half).join(" ");

console.log(selected);
</div> </div>
0
qo'shib qo'ydi
Javascript UZB
Javascript UZB
99 ishtirokchilar

@js_uzb @vuejs_uz @react_uz @nodejs_uz @angular_uz @ngTashkent @yiiframework_uz @laravel_uz @linux_uzbek @python_uz @swift_uzb —————— @uzdevgroup @UzGeeksGroup ——— @UzDev_Jobs @jobs_uzb