Динамический сгенерированный стиль стиля кнопки не работает

Функция:

public float simpleSimilarity(String u, String v) {
    String[] a = u.split(" ");
    String[] b = v.split(" ");

    long correct = 0;
    int minLen = Math.min(a.length, b.length);

    for (int i = 0; i < minLen; i++) {
        String aa = a[i];
        String bb = b[i];
        int minWordLength = Math.min(aa.length(), bb.length());

        for (int j = 0; j < minWordLength; j++) {
            if (aa.charAt(j) == bb.charAt(j)) {
                correct++;
            }
        }
    }

    return (float) (((double) correct) / Math.max(u.length(), v.length()));
}

Тест:

String a = "This is the first string.";

String b = "this is not 1st string!";

// for exact string comparison, use .equals

boolean exact = a.equals(b);

// For similarity check, there are libraries for this
// Here I'll try a simple example I wrote

float similarity = simple_similarity(a,b);
0
задан Narendra Jadhav 13 July 2018 в 10:57
поделиться

2 ответа

Я думаю, вы не добавляете кнопки в свой элемент section. С вашего CSS стили, которые вы хотите, будут применяться только к кнопкам в элементе section. Но вы добавляете динамически созданные кнопки к документу body.

Попробуйте дать вашему section элементу id, ссылаться на него, а затем добавить к нему динамически созданные кнопки.

Что-то вроде этого должно работать:

let buttons = [];
let section = document.getElementById("section");
const CLS = ["thin", "button"];
let add_btns = document.querySelector('.add-btn');
add_btns.addEventListener("click", function() {
  let i = 0;
  while (i <= 40) {
    let btn = document.createElement("BUTTON");
    buttons.push(section.appendChild(btn));
    i++;
    for (let button in buttons) {
      btn.setAttribute('id', "button " + button);
      btn.classList.add(...CLS);
      btn.innerHTML = "Button # " + button;
    }
    btn.addEventListener("click", function() {
      let id = this.id;
      alert("my id is: " + id);
    });
  }
});
html h1,
body h1 {
  margin-top: -5rem;
  text-align: center;
  color: #41403E;
  font-size: 3rem;
}

html section,
body section {
  display: flex;
  flex-direction: row;
  justify-content: center;
  margin-bottom: 3rem;
}

html section button,
body section button {
  align-self: center;
  background: transparent;
  padding: 1rem 1rem;
  margin: 0 1rem;
  transition: all .5s ease;
  color: #41403E;
  font-size: 2rem;
  letter-spacing: 1px;
  outline: none;
  box-shadow: 20px 38px 34px -26px rgba(0, 0, 0, 0.2);
  border-radius: 255px 15px 225px 15px/15px 225px 15px 255px;
}

html section button:hover,
body section button:hover {
  box-shadow: 2px 8px 4px -6px rgba(0, 0, 0, 0.3);
}

html section button.dotted,
body section button.dotted.thick {
  border: dotted 5px #41403E;
}

html section button.thin,
body section button.lined.thin {
  border: solid 2px #41403E;
}
<section id="section">
  <button class="add-btn">
Button
</button>
</section>

2
ответ дан AndroidNoobie 17 August 2018 в 13:08
поделиться

Как вы можете видеть из примера. Селекторы работают, когда они динамически генерируются. Я считаю, что у вас есть проблема с вложенными селекторами в css.

let buttons = [];
const CLS = ["thin", "button"];
let add_btns = document.querySelector('.add-btn');
add_btns.addEventListener("click", function () {
   let i = 0;
  while (i <= 40) {
      let btn = document.createElement("BUTTON");
      buttons.push(document.body.appendChild(btn));
      i++;
      for (let button in buttons) {
          btn.setAttribute('id', "button " + button);
          btn.classList.add(...CLS);
          btn.innerHTML = "Button # " + button;
      }
      btn.addEventListener("click", function () {
          let id = this.id;
          alert("my id is: " + id);
      });
  }
});
.button {
  padding: 10px;
}

.thin {
  padding: 0px 10px;
}
<button class="add-btn button">Add</button>

0
ответ дан Gregor Ojstersek 17 August 2018 в 13:08
поделиться
Другие вопросы по тегам:

Похожие вопросы: