小编rob*_*b.m的帖子

大写各种标签内容元素的首字母

我需要大写各种html元素的第一个字母,如p,h2,h3,h4.

这个堆栈问题帮助了我,但它只针对p标签.如何定位多个标签呢?

如:

function change(charm) {
   var string = eval(charm).toLowerCase(), change = "", newstring = string.split("."),   tochange;
   for (var i = 0; i < newstring.length; i++) {
       tochange = newstring[i].trim().charAt(0);
       newstring[i] = newstring[i].replace(tochange, tochange.toUpperCase());
}
   change = newstring.join(" . ");
   return change;
}

var text = change("document.getElementsByTagName('p, h2, h3, h4')[0].innerHTML");
document.getElementsByTagName("p, h2, h3, h4")[0].innerHTML = text;
Run Code Online (Sandbox Code Playgroud)

这不起作用,我在针对各种元素做错了吗?

javascript css jquery

2
推荐指数
1
解决办法
610
查看次数

我在哪里插入ajax调用中的标题?

我试过了:

$("#wiki").on('click', function(e) {
  var q = $(this).data('subject');
  $.getJSON("https://it.wikipedia.org/w/api.php?gsrnamespace=0&gsrsearch=test&gsrlimit=10&prop=pageimages|extracts&pilimit=max&exintro&explaintext&exsentences=1&exlimit=max?callback=?", {
    srsearch: q,
    action: "query",
    list: "search",
    prop: "text",
    section: 0,
    format: "json"
  }, function(data) {
      headers: {
        "Accept" : "application/json; charset=utf-8",
        "Content-Type": "application/javascript; charset=utf-8",
        "Access-Control-Allow-Origin" : "*"
      },
      $("#results").empty();
      $("#results").append("<p>Results for <b>" + q + "</b></p>");
      $.each(data.query.search, function(i, item) {
      $("#results").append("<div><a href='http://it.wikipedia.org/wiki/" + encodeURIComponent(item.title) + "'>" + item.title + "</a><br>" + item.snippet + "<br><br></div>");
    });
  },
  );
});
Run Code Online (Sandbox Code Playgroud)

$("#wiki").on('click', function(e) {
  var q = $(this).data('subject');
  $.getJSON("https://it.wikipedia.org/w/api.php?gsrnamespace=0&gsrsearch=test&gsrlimit=10&prop=pageimages|extracts&pilimit=max&exintro&explaintext&exsentences=1&exlimit=max?callback=?", {
    srsearch: q, …
Run Code Online (Sandbox Code Playgroud)

ajax jquery

2
推荐指数
1
解决办法
62
查看次数

如何检查文本中的任何单词是否在数组中?

给出一个完整的文字,如:

var nation = "Piazza delle Medaglie d'Oro
40121 Bologna
Italy"
Run Code Online (Sandbox Code Playgroud)

和给定的数组如:

 ["Afghanistan", "Italy", "Albania", "United Arab Emirates"]
Run Code Online (Sandbox Code Playgroud)

我们怎样才能检查整篇文章中的意大利语array什么?

在这个SO答案之后,这就是我尝试过的,但是我得到的False却是意大利人array

  var countries = [];
  $("#usp-custom-3 option").each(function() {
    var single = $(this).text();
    countries.push(single);
    var foundPresent = countries.includes("Piazza delle Medaglie d'Oro 40121 Bologna Italy");
    console.log(foundPresent); 
  });
Run Code Online (Sandbox Code Playgroud)

JsFiddle在这里

javascript jquery

2
推荐指数
1
解决办法
148
查看次数

我们能找到2个班吗?

我想知道实现这个目标的最佳方法是什么:

var $optionSets = $('#options').find('.option-set','#talents')..
Run Code Online (Sandbox Code Playgroud)

这不起作用.

基本上我想说要分配var $optionSets.option-set#talents

javascript jquery

1
推荐指数
1
解决办法
47
查看次数

如何从特定元素中删除 fastclick?

我正在使用快速点击,我通过以下方式将其应用于身体:

FastClick.attach(document.body);
Run Code Online (Sandbox Code Playgroud)

我如何不将它应用于像这样的元素:

<div class="item">My El</div> -- Do not apply fastclick to this element
Run Code Online (Sandbox Code Playgroud)

更新

错过了告诉如何操作的文档

javascript jquery fastclick.js

1
推荐指数
1
解决办法
2873
查看次数

如何用php中的给定字符串替换带有空格的字符?

$country = preg_replace(array('/^\[/','/\]$/'), '',$country); 
Run Code Online (Sandbox Code Playgroud)

根据此SO问题,这将删除文本中任何位置的括号

但是我们说:

United_arab_emirates
Run Code Online (Sandbox Code Playgroud)

我们怎样才能将它作为:

United Arab Emirates

注意:这里不是php大师*

这就是我所拥有和尝试过的(感谢其中一个答案)

$custom_country = usp_get_meta(false, 'usp-custom-3');
$custom_country = htmlspecialchars_decode($custom_country);
$custom_country = nl2br($custom_country);
$country = preg_replace(array('_'), ' ',$country); 
echo ucfirst($custom_country);
Run Code Online (Sandbox Code Playgroud)

结果还是 United_arab_emirates

php regex

1
推荐指数
1
解决办法
47
查看次数

如何比较两个字符串而不管它们的大小写?

我有:

mytext = jQuery('#usp-title').val();
Run Code Online (Sandbox Code Playgroud)

那我呢

if(jQuery("#datafetch h2 a").text() == mytext) {
Run Code Online (Sandbox Code Playgroud)

但是一个标题可能是:

Space Mission 同样如此 space mission

由于其他原因,我无法标准化文本格式,因此我真的在考虑比较两个字符串而不管它们的大小写.

UPDATE

问题是我使用输入字段作为搜索,因此用户可以键入,space mission而帖子标题将是,Space mission因此搜索结果将不准确,我需要确切的短语.

完整代码:

<div id="datafetch"></div>

function fetch(){
  jQuery('#datafetch').empty();
  mytext = jQuery('#usp-title').val();
    jQuery.ajax({
        url: '<?php echo admin_url('admin-ajax.php'); ?>',
        type: 'post',
        data: { action: 'data_fetch', exactwords:  mytext },
        success: function(data) {
            jQuery('#datafetch').html( data );
        if(jQuery("#datafetch h2 a").text().toLowerCase().trim() == mytext.toLowerCase().trim()) {
            jQuery("#componi").attr("disabled", "disabled").hide();
        } else {
                jQuery("#componi").removeAttr("disabled", "disabled").show();
        }
            if(jQuery("#datafetch h2 a").text() != mytext) {
            jQuery("#fatto").hide();
            jQuery('#datafetch').empty();
        }
        } …
Run Code Online (Sandbox Code Playgroud)

javascript jquery

1
推荐指数
1
解决办法
330
查看次数

我怎么能动态创建一个阵列?

我有以下内容:

    $("#years").ionRangeSlider({
      type: "double",
      grid: true,
      min: 0,
      from: 10,
      to: 11,
      max: 2018,
      prettify_enabled:false,
      values: [
          "1910", "1920", "1930",
          "1940", "1950", "1960",
          "1970", "1980", "1990",
          "2000", "2010", "2018"
      ]
    });
Run Code Online (Sandbox Code Playgroud)

我需要能够from 0 to 2018在这部分生成

      values: [
          "1910", "1920", "1930",
          "1940", "1950", "1960",
          "1970", "1980", "1990",
          "2000", "2010", "2018"
      ]
Run Code Online (Sandbox Code Playgroud)

但我不能手动,因为它们是2千个值.所以我想到了一个循环,比如说

for(var i = 0; i < 2018; i++;) {

} 
Run Code Online (Sandbox Code Playgroud)

但是,我不确定如何生成该对象

      [
          "0", .... , "2018"
      ]
Run Code Online (Sandbox Code Playgroud)

javascript jquery

1
推荐指数
1
解决办法
83
查看次数

n号后如何计算和隐藏元素?

我有以下HTML

   <div id="area">
     <p>Paragraph one</p>
     <p>Paragraph two</p>
     <p>Paragraph three</p>
     <p>Paragraph four</p>
     <p>Paragraph five</p>
     <p>Paragraph six</p>
   </div>
Run Code Online (Sandbox Code Playgroud)

我需要hide所有<p>标签后,first one并有一个更多/更少阅读按钮.

我知道我可以通过这样做来计算元素:

var count = $("#area").find("p").length;
if(count > 1) {

}
Run Code Online (Sandbox Code Playgroud)

但是,如何更多/更少地插入读取?

试图实现显示/隐藏切换显示块/无:

   <div id="area">
     <p>Paragraph one</p>
     <button type="button">Read more</button>
     <p style="display: none;">Paragraph two</p>
     <p style="display: none;">Paragraph three</p>
     <p style="display: none;">Paragraph four</p>
     <p style="display: none;">Paragraph five</p>
     <p style="display: none;">Paragraph six</p>
   </div>
Run Code Online (Sandbox Code Playgroud)

javascript jquery

0
推荐指数
1
解决办法
89
查看次数

如何控制台日志输入值?

我正在尝试将a应用于元素中的input.val()a textspan但是当我尝试对其进行检查时却console没有得到。

$("#sandbox-container span").on("click", function(e) {
      $("#sandbox-container span").removeClass("selected_year");
      $(this).attr("class", "selected_year");
      var dateSelected = $(".selected_year").text();
      var dateCat = $(".time_value").val(dateSelected);
      console.log(dateSelected);
      console.log(dateCat);
    });
Run Code Online (Sandbox Code Playgroud)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="sandbox-container">
      <span class="selected_year">2014</span>
      <span>2018</span>
    </div>
<input label="null" style="display: none;" class="time_value" maxlength="99" name="usp-category" type="text" value="">
Run Code Online (Sandbox Code Playgroud)

上面给了我:

2014
(index):208 r.fn.init [prevObject: r.fn.init(1)]
Run Code Online (Sandbox Code Playgroud)

虽然我希望console.log(dateCat);给我2014

javascript jquery

0
推荐指数
1
解决办法
1551
查看次数

标签 统计

jquery ×9

javascript ×8

ajax ×1

css ×1

fastclick.js ×1

php ×1

regex ×1